Selenium Cookbook lesson
Selenium In A Nutshell
Selenium is a software robot sent from the future to help us test web applications. Selnium works on every major browser and run on everywhere. One of the guiding philosophies of Selenium is to be able to write your tests once and run them across multiple browsers.
Defining A Test Strategy
- Money/Value
- Usage Data
- Browsers
- Risky Bits
Anatomy of A Good Acceptance Test
- Write atomic and autonomous tests
Each test needs to be concise and be capable of being run independently
- Grouping Tests
- Being Descriptive
- Use a Test Runners
- Store tests using Version Control
Writing Test
- Find Quality Elements
- id / class attributes
- css selectors
- the parent element with a unique element that houses them
- if no unique element, ask development team for help (retrospective)
- Figure Out What To Assert implicitly
- follow the test cases and add necessary additional assert
- Write The Assertion And Verify it
Writing Re-usable Test Code
Model the behavior of your application into simple objects and write your tests against them, which not only get the benefit of controlled chaos, we also get reusable functionality and more readable tests.
- Base Page Object
- abstracting all of common actions into a central class
Writing Resilient Test Code
There are still some issues to deal with. Most notably…timing. This is especially true when working with dynamic, JavaScript heavy pages. The best way to accomplish this is through the use of explicit waits.
- use Explicit waits instead of implicit wait or Thread.sleep()
- adjust the time for different browsers
Selenium Code Best Practice
Prepping For Use
- Global Setup & Teardown
- This rule has before and after methods that execute prior to methods annotated with @Before and @After. It will not give up the ability to use these annotations in the tests.
public class Base {
protected WebDriver driver;
@Rule
public ExternalResource resource = new ExternalResource() {
@Override
protected void before() throws Throwable {
super.before();
//Driver.initialise(browser, executeBy);
}
@Override
protected void after() {
super.after();
//driver.quit();
}
}
}
- Using TestWatcher
- this rule will be able to get the testName, get the screenshot for fail test cases and quit the driver.
@Rule
public TestWatcher watcher = new TestWatcher() {
@Override
protected void starting(Description description) {
testName = description.getDisplayName();
}
@Override
protected void failed(Throwable e, Description description) {
String methodName = description.getMethodName();
String fileName = description.getTestClass().getSimpleName() + "-" + methodName + ".png";
try {
File file = new File(
System.getProperty("user.home") + "/Desktop/Selenium/" + fileName + LocalDateTime.now().toString()
+ ".png");
FileUtils.copyFile(((TakesScreenshot) Driver.getInstance()).getScreenshotAs(OutputType.FILE), file);
} catch (IOException ioe) {
LOG.info("Exception caught: {}", e.getMessage());
}
}
@Override
protected void finished(Description description) {
Driver.getInstance().quit();
}
};
- Speeding Up Test Runs using Maven Surefire plugin (Todo in sls)
- Flexible Test Execution
- create Categories to group tests
- Using Selenium Grid
With Selenium Grid you can stand up a simple infrastructure of various browsers on different operating systems to not only distribute test load, but also give you a diversity of browsers to work with.
- Grid Setup
- start the hub
- register nodes to it
- specify an additional -browser parameter
java -jar selenium-server-standalone-2.48.2.jar -role node -browser browserName=firefox -browser browserName=chrome -browser browserName=safari -hub http://localhost:4444/grid/register
- Wire up a test to use new Grid
public class Grid {
WebDriver driver;
@Before
public void setUp() throws Exception {
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
String url = "http://localhost:4444/wd/hub";
driver = new RemoteWebDriver(new URL(url), capabilities);
}
@After
public void tearDown() throws Exception {
driver.quit();
}
@Test
public void gridTest() {
driver.get("http://the-internet.herokuapp.com/");
assertThat(driver.getTitle(), is(equalTo("The Internet")));
}
}