Selenium tests, the Object Oriented way

Preview:

Citation preview

Selenium tests, the Object Oriented way

Corina Pip@imalittletesterhttps://iamalittletester.wordpress.com/

• Blog: https://iamalittletester.wordpress.com/ • Travel blog: https://travelwithcori.com/ • Photos: https://www.flickr.com/photos/capreoara • Twitter: @imalittletester

@imalittletester

What problem do we want to solve?

•Testing of modules that appear in several places

•Testing of the same module in several languages

•Testing of a module that is complex

@imalittletester

Some examples

•Rating/review modules

•Similar products module on shopping sites

•Google search results

@imalittletester

The ‘classic’ approach

•A large number of assertions•One assertion for each property

•Code is heavy•Updating code is tricky

@imalittletester

The proposed solution

•See everything as objects•Map your expected content into an object•Map your actual content into an object•Compare the objects

•Identify the objects on your page•Top down vs. Bottom up

@imalittletester

Top down approach

•Break the bigger modules into smaller ones

•Break the smaller modules into tiny ones

•Break it down down down until you get to

primitives and Strings@imalittletester

Bottom up approach

•Build tiny modules out of primitives and

Strings

•Build small modules out of the tiny ones

•Larger modules include the small ones@imalittletester

An object – key actors

• The fields• To emulate the attributes

• The constructors• To generate the expected content• To generate the actual content

• The methods: equals, hashCode, toString• To compare the expected and actual + print the objects

@imalittletester

The Link

@imalittletester

The Link

<a id="awesomeLink" href="http://iamalittletester.wordpress.com/"

target="_blank">Read about testing

</a>

@imalittletester

The Link object

@imalittletester

public class Link { //the properties private String linkUrl;

private String linkTarget;

private String linkLabel;

@imalittletester

//the constructor for the EXPECTED contentpublic Link(String linkUrl, String linkTarget, String linkLabel) {

this.linkUrl = linkUrl;

this.linkTarget = linkTarget;

this.linkLabel = linkLabel;}

@imalittletester

//the constructor for the ACTUAL contentpublic Link(WebElement linkElement) {

this.linkUrl = linkElement.getAttribute("href");

this.linkTarget = linkElement.getAttribute("target");

this.linkLabel = linkElement.getText();}

… + equals() + hashCode() + toString() }

The test

@imalittletester

private Link expectedTestingBlogLink = new Link("http://iamalittletester.wordpress.com/","_blank", "Read about testing");

The test

@imalittletester

@Testpublic void linkTest() { //data preparation steps here assertEquals(

new Link(page.testBlogLinkWebElement), expectedTestingBlogLink);

}

The ReadingModule

@imalittletester

The ReadingModule HTML

<div class=“readingModule”><img src="https://c1.staticflickr.com/8/7494/27510788280_a9f43c3845_s.jpg"

width="75" height="75" >

<h2>Go ahead. Read something!</h2><a

id="awesomeLink" href="http://iamalittletester.wordpress.com/" target="_blank">Read about testing

</a> </div>

@imalittletester

The ReadingModule object

@imalittletester

private Image image;

private String h2Text;

private Link testingBlogLink;

The ReadingModule object

@imalittletester

//for creating the expected contentpublic ReadingModule(Image image, String h2Text,

Link testingBlogLink) { this.image = image; this.h2Text = h2Text; this.testingBlogLink = testingBlogLink;}

The ReadingModule object

@imalittletester

//for creating the actual contentpublic ReadingModule(WebElement moduleElement) { this.image =

new Image(moduleElement.findElement(tagName("img"))); this.h2Text =

moduleElement.findElement(tagName("h2")).getText(); this.testingBlogLink =

new Link(moduleElement.findElement(tagName("a")));}

The ReadingModule test

@imalittletester

private Link expectedTestingBlogLink = new Link("https://iamalittletester.wordpress.com/", "_blank", "Read about testing");

private Image expectedImage = new Image(“someUrlHere", "75", "75");

private ReadingModule expectedReadingModule = new ReadingModule(expectedImage,

"Go ahead. Read something!", expectedTestingBlogLink);

The ReadingModule test

@imalittletester

@Testpublic void readingModuleTest() { //test data processing here assertEquals(

new ReadingModule(page.readingModuleDiv), expectedReadingModule);

}

IDE results

• When assert fails:

@imalittletester

Summary

• Instead of writing a bunch of assertions, perform

object analysis

• Identify and model your objects

•Compare objects

@imalittletester

The PROs

•Small number of assertions in tests small tests

•Object can be used to generate lots of content

•Once expected object defined, can be reused

@imalittletester

The CONs

•Failure console output difficult to read in case of

large objects

•Not following good naming conventions

confusion when reading object properties

@imalittletester

@imalittletester

Got some questions?

Thank you!

Recommended