Tuesday 19 July 2016

Selenium to Selenide Transition



1. Create a browser

Selenium WebDriver:
DesiredCapabilities desiredCapabilities = DesiredCapabilities.htmlUnit();
desiredCapabilities.setCapability(HtmlUnitDriver.INVALIDSELECTIONERROR, true);
desiredCapabilities.setCapability(HtmlUnitDriver.INVALIDXPATHERROR, false);
desiredCapabilities.setJavascriptEnabled(true);
WebDriver driver = new HtmlUnitDriver(desiredCapabilities);

Selenide:
open("/my-application/login");
// And run tests with option -Dbrowser=htmlunit (or "chrome" or "ie", default value is "firefox")


2. Shutdown a browser
Selenium WebDriver:
if (driver != null) {
    driver.close();
}
Selenide:
// Do not care! Selenide closes the browser automatically.

3. Find element by id
Selenium WebDriver:
WebElement customer = driver.findElement(By.id("customerContainer"));
Selenide:
WebElement customer = $("#customerContainer");
or a longer conservative option:
WebElement customer = $(By.id("customerContainer"));

4. Assert that element has a correct text
Selenium WebDriver:
assertEquals("Customer profile", driver.findElement(By.id("customerContainer")).getText());

Selenide:
$("#customerContainer").shouldHave(text("Customer profile"));

5. Ajax support (waiting for some event to happen)
Selenium WebDriver (OMG!):
FluentWait<By> fluentWait = new FluentWait<By>(By.tagName("TEXTAREA"));
fluentWait.pollingEvery(100, TimeUnit.MILLISECONDS);
fluentWait.withTimeout(1000, TimeUnit.MILLISECONDS);
fluentWait.until(new Predicate<By>() {
    public boolean apply(By by) {
        try {
            return browser.findElement(by).isDisplayed();
        } catch (NoSuchElementException ex) {
            return false;
        }
    }
});
assertEquals("John", browser.findElement(By.tagName("TEXTAREA")).getAttribute("value"));
Selenide:
$("TEXTAREA").shouldHave(value("John"));
This command automatically waits until element gets visible AND gets expected value.
Default timeout is 4 seconds and it's configurable.

6. Assert that element has a correct CSS class
Selenium WebDriver:
assertTrue(driver.findElement(By.id("customerContainer")).getAttribute("class").indexOf("errorField") > -1);
Selenide:
$("#customerContainer").shouldHave(cssClass("errorField"));

7. Find element by text
Selenium WebDriver:
No way (except XPath)
Selenide:
WebElement customer = $(byText("Customer profile"));

8. Assert that element text matches a regular expression
Selenium WebDriver:
WebElement element = driver.findElement(By.id("customerContainer"));
assertTrue(Pattern.compile(".*profile.*", DOTALL).matcher(element.getText()).matches());

Selenide:
$("#customerContainer").should(matchText("profile"));

9. Assert that element does not exist
Selenium WebDriver:
try {
    WebElement element = driver.findElement(By.id("customerContainer"));
    fail("Element should not exist: " + element);
}
catch (WebDriverException itsOk) {}
Selenide:
$("#customerContainer").shouldNot(exist);

10. Looking for element inside parent element
Selenium WebDriver:
WebElement parent = driver.findElement(By.id("customerContainer"));
WebElement element = parent.findElement(By.className("user_name"));
Selenide:
$("#customerContainer").find(".user_name");

11. Looking for Nth element
Selenium WebDriver:
WebElement element = driver.findElements(By.tagName("li")).get(5);
Selenide:
$("li", 5);

12. Click "Ok" in alert dialog
Selenium WebDriver:
    try {
      Alert alert = checkAlertMessage(expectedConfirmationText);
      alert.accept();
    } catch (UnsupportedOperationException alertIsNotSupportedInHtmlUnit) {
      return;
    }
    Thread.sleep(200); // sometimes it will fail
Selenide:
    confirm("Are you sure to delete your profile?");
or
    dismiss("Are you sure to delete your profile?");
13. Debugging info for elements
Selenium WebDriver:
    WebElement element = driver.findElement(By.id("customerContainer"));
    System.out.println("tag: " + element.getTag());
    System.out.println("text: " + element.getText());
    System.out.println("id: " + element.getAttribute("id"));
    System.out.println("name: " + element.getAttribute("name"));
    System.out.println("class: " + element.getAttribute("class"));
    System.out.println("value: " + element.getAttribute("value"));
    System.out.println("visible: " + element.isDisplayed());
    // etc.
Selenide:
    System.out.println($("#customerContainer"));
    // output looks like this: "<option value=livemail.ru checked=true selected:true>@livemail.ru</option>"

14. Take a screenshot
Selenium WebDriver:
    if (driver instanceof TakesScreenshot) {
      File scrFile = ((TakesScreenshot) webdriver).getScreenshotAs(OutputType.FILE);
      File targetFile = new File("c:\temp\" + fileName + ".png");
      FileUtils.copyFile(scrFile, targetFile);
    }
Selenide:
    takeScreenShot("my-test-case");
For JUnit users it's even more simpler:
    public class MyTest {
      @Rule // automatically takes screenshot of every failed test
      public ScreenShooter makeScreenshotOnFailure = ScreenShooter.failedTests();    }
15. Select a radio button
Selenium WebDriver:
    for (WebElement radio : driver.findElements(By.name("sex"))) {
      if ("woman".equals(radio.getAttribute("value"))) {
        radio.click();
      }
    }
    throw new NoSuchElementException("'sex' radio field has no value 'woman'");
Selenide:
    selectRadio(By.name("sex"), "woman");

16. Reload current page
Selenium WebDriver:
    webdriver.navigate().to(webdriver.getCurrentUrl());
Selenide:
    refresh();

17. Get the current page URL, title or source
Selenium WebDriver:
    webdriver.getCurrentUrl();
    webdriver.getTitle();
    webdriver.getPageSource();
Selenide:
    url();
    title();

    source();

My first program with Selenide:


Create a Maven project.

Add selenium-java client dependencies in pom.xml

Add TestNG or JUnit dependencies in pom.xml

And finally Selenide dependencies

<dependency>
    <groupId>com.codeborne</groupId>
    <artifactId>selenide</artifactId>
    <version>3.7</version>
    <scope>test</scope>
</dependency>


Add a simple TestNG/ Junit class

import static com.codeborne.selenide.Selenide.*;
import static com.codeborne.selenide.Condition.*;
public class Test
{
@Test
public void userCanLoginByUsername() {
  open("/login");
  $(By.name("user.name")).setValue("johny");
  $("#submit").click();
  $(".loading_progress").should(disappear); 
  $("#username").shouldHave(text("Hello, Johny!")); 
}
}
 


Just Run it.

What is Selenide?


Wrapper around Selenium Webdriver for below reasons.

Open Source, Stable and Concise API

Concise API: Very less code, we don’t need to write code to start and stop web driver.

Ajax based locator support by using $, $$

Smart Waits using ’shouldBe(visible)’, ‘ShouldHave’, ShouldNotHave

Multi file upload and Downloading a file is very easy.

Finds parent, child, sibling etc. elements very easily.

Selenide profiler helps to find execution time for each step.

Execution is very fast.

Many more features.

You can still use Webdriver directly.

Solved below issues:

Ajax issues, Time outs, Stale Element Exceptions and easy Screenshots

Supports all other features:

Parallel execution

POM design pattern

Use with Junit, TestNg, jBehave, ScalaTest

http://selenide.org/

here is sample code : https://github.com/kasthurikranthikumar/selenide.git