Cypress V/S Selenium

 

Cypress V/S Selenium

During my learning of Cypress, I noted down a few important differences between these tools.

Documentation: Cypress Selenium

FeatureCypressSelenium
Language SupportJavaScriptMultiple languages including Java, C#, etc.
Test ArchitectureMocha test runner and Chai assertion library built-inRequires separate test frameworks and assertion libraries
Browser SupportChrome, Firefox, ElectronMultiple browsers including Chrome, Firefox, Safari, Edge, etc.
Test StructureBuilt-in test runner and assertion libraryNeeds separate test runners and assertion libraries
Test ExecutionFaster executionSlower execution due to the use of WebDriver
DOM ManipulationNative commands for DOM manipulationNeeds to use JavaScriptExecutor for DOM manipulation
LocatorsCypress uses jQuery-like syntax for selecting elementsSelenium uses various locating strategies such as XPath, CSS Selector, etc.
Automatic WaitsAutomatically waits for commands to complete before moving onRequires explicit wait statements
DebuggingCypress provides an interactive debuggerSelenium does not have built-in interactive debugger
Cross-browser TestingLimited cross-browser testing supportStrong cross-browser testing support
Community SupportCypress has a smaller but active communitySelenium has a larger and more established community
Learning CurveEasier to learn and get started withSteeper learning curve compared to Cypress
Screenshot and Video CaptureBuilt-in commands for capturing screenshots and videos Requires additional code to capture screenshots
Dashboard SupportCypress Dashboard provides test run visualization and analyticsSelenium does not have built-in dashboard support

Assertions

Assertion TechniqueCypressSelenium
Built-in Assertion LibraryCypress provides a built-in assertion library
cy.get('h1').should('have.text', 'Welcome to my App')
Selenium does not have a built-in assertion library
Assert.assertEquals("Welcome to
my App", driver.findElement(
By.tagName("h1")).getText())
ChainingCypress allows chaining of commands for easy assertions
cy.get('input').type('test').should
('have.value', 'test')
Selenium does not support chaining of commands for assertions
driver.findElement(By.id("input")).
sendKeys("test");
assertEquals("test", driver.findElement(By.id("input")).
getAttribute("value"));
Async/AwaitCypress supports async/await syntax for assertions
await cy.get('button').click()
Selenium does not support async/await syntax for assertions.
WebElement button = driver.findElement(By.id("button"));
WebElement button = driver.findElement(By.id("button"));
WebElement button = driver.findElement(By.id("button"));
element.click();
Error MessagesCypress provides detailed error messages for easy debugging
cy.get('input').should('have.attr', 'required')
Selenium error messages may be more cryptic and difficult to debug
assertTrue(driver.findElement(By.
id("input")).getAttribute("required") != null);
NegationCypress allows easy negation of assertions with the not keywordSelenium requires the use of the assertNot method for negation of assertions
Custom AssertionsCypress allows for easy creation of custom assertionsSelenium requires more setup for creating custom assertions
RetryCypress automatically retries assertions until they pass or time outSelenium does not have built-in retry functionality for assertions



Assertion TechniqueCypressSelenium
Equalityexpect(foo).to.equal(bar)assertEquals(foo, bar)
Existencecy.get('.selector').should('exist')assertTrue(driver.findElement
(By.cssSelector(".selector")).
isDisplayed())
Visibilitycy.get('.selector').should('be.visible')assertTrue(driver.findElement
(By.cssSelector(".selector")).
isDisplayed())
Behavioralcy.get('.selector').should('have.class', 'active')

cy.get('.selector').should('have.css', 'background-color', 'rgb(255, 0, 0)')

cy.get('.selector').should('contain.text', 'Hello, World!')
Actions actions = new Actions(driver);

WebElement element = driver.findElement(By.
cssSelector(".selector"));

actions.moveToElement
(element).perform();

assertTrue(element.
getAttribute("class").
contains("active"));

assertEquals("rgb(255, 0, 0)", element.getCssValue
("background-color"));

assertEquals(true, element.getText().contains
("Hello, World!"))






Comments

Popular posts from this blog

Selenium: File download handling.

Major Differences between Python and Java