Thursday, 20 August 2015

1. Introduction

1. Introduction

  1. 10-minutes video
  2. Cross browser testing
  3. How to run Selenide with custom Chrome/Firefox profile
  4. What if I miss for "raw" Selenium but don't want to dismiss Selenide?
  5. What about debugging test scripts?

  • Where can I find screenshots if test fails
  • Leave browser open after test finishes for further investigation

    6. Hand-made: Selenide cheat sheet


1. 10-minutes video

It is assumed that you watched this cool 10-minutes video and were able to successfully complete the steps. 


2. Cross browser testing

You can run Selenide with another browser by passing "browser" parameter when running tests.
Lets try run tests with Chrome from command line:

mvn test -Dbrowser=chrome

I hope your you have set path to the driver executable and the chrome window is opened. If not - fix it.  This mean that when you run in terminal "chromedriver" you get something like that:

Starting ChromeDriver 2.16.333243 (0bfa1d3575fc1044244f21ddb82bf870944ef961) on port 9515
Only local connections are allowed.



3. How to run Selenide with custom Chrome/Firefox profile

Selenide wiki shows an example for Firefox profile:

You need to create CustomWebDriverProvider class and define the path to the firefox profile.

  public static class CustomWebDriverProvider implements WebDriverProvider {
    @Override
    public WebDriver createDriver(DesiredCapabilities capabilities) {
       FirefoxProfile profile = new FirefoxProfile(new File("/home/test/MozzillaProf/"));
       profile.setAssumeUntrustedCertificateIssuer(false);
       profile.addAdditionalPreference("general.useragent.override", "some UA string");

       capabilities.setCapability(FirefoxDriver.PROFILE, profile);
       return new FirefoxDriver(capabilities);
    }
  }

Then you can pass Selenide a name of factory class that creates WebDriver instance by your needs:

mvn test -Dbrowser=com.mycompany.CustomWebDriverProvider


Lets try to run tests with custom Chrome profile.
To provide a little bit complicated example we create Chrome WebDriver instance with extention ( *.crx). I have chosen BugMagnet.


public class CustomWebDriverProviderChrome implements WebDriverProvider {
    public WebDriver createDriver(DesiredCapabilities capabilities) {
    
      ChromeOptions chromeOptions = new ChromeOptions();
      chromeOptions.addArguments("user-data-dir=./src/test/profiles/chrome/testProfile/");
      chromeOptions.addExtensions(new File("./src/test/profiles/chrome/extentions/bugmagnet.crx"));
      chromeOptions.addArguments("--start-maximized");
      chromeOptions.addArguments("disable-popup-blocking", "true");
      
      capabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions);
     
       return new ChromeDriver(capabilities);
    }
  }


And run it:
mvn test -Dbrowser=com.mycompany.CustomWebDriverProviderChrome

4. What if I miss for "raw" Selenium?

Ok, if you need webdriver instance just type "getWebDriver()" and work with it in usual way.

For example, add this code in your test to minimize browser window during test execution. 

import static com.codeborne.selenide.WebDriverRunner.getWebDriver;
import org.openqa.selenium.Dimension;

getWebDriver().manage().window().setSize(new Dimension(0,0));


5. What about debugging test scripts?

  • Where can I find screenshots if test fails?

Screenshots are taken on each test fail and in terminal output you can find something like that:

Screenshot: file:/path/to/project-name/build/reports/tests/1440105486375.0.png

By the way it is the folder for file downloads too.

  • Leave browser open after test finishes

Sometimes it useful to run test that fails and  keep the window open for further investigations. As you can see from tutorial video there is no before( or setUp) and after(or tearDown)  methods in test code. Selenide does all work with creating and closing browser. 

To change it just add this code to your test before assertions:

import static com.codeborne.selenide.Configuration.holdBrowserOpen;


holdBrowserOpen = true;





2 comments:

  1. Creating custom driver provider leads to:
    The path to the driver executable must be set by the webdriver.chrome.driver system property;

    So it is impossible to use clean Selenide. you need to download driver and set path to it trough system.setProperty("webdriver.chrome.driver" ,"path/chromedriver.exe")

    ReplyDelete