Skip to content

Cypress Vs Selenium (Everything You Need To Know)

Cypress Vs Selenium
Cypress vs Selenium

Selenium and cypress both are test automation tools used for web applications. Selenium is a well-established player while Cypress is comparatively new in the test community, however, it has gained lots of popularity in no time. Nowadays it is a very prominent question Which tool is better cypress or selenium(Cypress Vs Selenium)? In my opinion, it totally depends on your requirement, and if you are well-versed in one automation framework then it is not going to be difficult to learn another one. In this article, I have tried to list down the similarities and differences between both frameworks.

1)Commands:

FunctionSeleniumCypress
Launch the URL in the Browserdriver. get(URL)cy.visit(URL)
Locate the web element driver.findElement()cy.get()
Wait for a specified period of timethread.sleep(time)cy.wait()
Enter text in the Textboxdriver.sendKeys()cy.type()
To Check the visibility of the elementdriver.findElement().isDisplayed()cy.get(‘selector’).should(‘be.visible’)
To Check/Assert Text(Assertion)assertEquals(driver.findElement.getText(),expected text)cy.get(‘selector’).should(‘have.text’,text)
Check Checkboxdriver.findElement().click()cy.get(‘[type=”checkbox”]’).check()
Clear or reset an input field.clear()cy.get().clear()
Perform Double ClickUse actions classcy.get(‘button’).dblclick()
cy.contains(”).dblclick()
Reload/Refresh pagedriver. navigate().refresh()
cy.reload()
Cookiesdriver.manage().deleteAllCookies();cy.clearCookies()

2)Key Differences

SeleniumCypress
Supported languageJava, c#, Python, JavaScript, RubyJavaScript
Supported BrowserChrome, Firefox, Safari, EdgeChrome, Electron, Firefox
Supported Test frameworksMochaJS, Jest, WebDriverIO, and other wrappers like SelenoidMochaJS
Type of Tests Integration Tests, E2E tests, UI testingE2E, Unit Tests
Community SupportRobust communityEmerging

3)Key Strengths and ShortComings

a)Selenium:

StrengthDisadvantage
Cross-browser and Cross-platformThe flakiness of the tests
Multilanguage SupportSpeed, Execution time
Testing of Mobile applications, Availability of several wrapper tools, Strong Community Support Lots of Configuration is required to SetUp the test environment
Support of Parallel testing and Cloud platforms integrationNo Built-in command for the automatic generation of test results.

b)Cypress:

StrengthDisadvantage
Automatic wait handling hence the execution is fastDoes not provide support for multi-tabs
Time Travel(Captures screenshot at the time of execution)Two browsers can not be driven at the same time
Does not require additional configuration or dependencies.Limited support for Iframes
No network lag or test flakiness Tests can be written only in JavaScript
Cypress auto-accepts alerts and popupSupport only CSS selector
Bundles with the Chai assertion library.Mouse Hover events are not supported, need to use JQuery or force click.

4)Architecture

Selenium web driver is not a standalone tool. It comprises Language bindings, JSON Wire Protocol, Browser Drivers, and Browses. It operates by running outside the browser and executing remote commands across the network. Selenium can run tests on the browsers only if they are locally installed, either on the local machine or on the server machines. So browser installation is necessary.

The Cypress engine directly operates inside the browser. In other words, It is the browser that is executing your test code. This enables Cypress to listen and modify the browser behaviour at run time by manipulating DOM and altering Network requests and responses on the fly.

5)Code Snippet(Cypress Vs Selenium)

Task:

  • Launch URL “https://Google.com”
  • A search query in the search bar
  • Print results

Cypress Code snippet:

describe('Cypress Test', () => {
  it('Google Search', () => {
    // Visit Google
    cy.visit('https://www.google.com/')

    // Get Search Bar and enter the search keyword
    cy.get('input[name="q"]').type('Cypress');

    // Submit the Search
    cy.get('input[value="Google Search"]').first().click();

    // Get Search Results
    cy.url().then(url => {
      const getUrl = url
      cy.log('Results URL: '+getUrl)
    })
  })
})

Selenium script:

public class GoogleSearch{
   public static void main(String[] args) {
      WebDriverManager.chromedriver().setup();
      driver = new ChromeDriver();
      driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
      driver.get("https://www.google.com/");
      // identify element
      WebElement p=driver.findElement(By.name("q"));
      //enter text with sendKeys() then apply submit()
      p.sendKeys("Selenium Java");
     
     
     p.submit();
      driver.close();
   }
}

Cypress is specifically targeted towards developers and QA engineers to facilitate test-driven development coupled with end-to-end testing. However, Cypress requires some programming experience in JavaScript to utilize the tool effectively. Furthermore, it will not be as feature-rich as Selenium and may require additional configurations and workarounds since it is a newer solution.