29
Selenium tests, the Object Oriented way Corina Pip @imalittletester https:// iamalittletester.wordpress.co m/

Selenium tests, the Object Oriented way

Embed Size (px)

Citation preview

Page 1: Selenium tests, the Object Oriented way

Selenium tests, the Object Oriented way

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

Page 2: Selenium tests, the Object Oriented way

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

@imalittletester

Page 3: Selenium tests, the Object Oriented way

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

Page 4: Selenium tests, the Object Oriented way

Some examples

•Rating/review modules

•Similar products module on shopping sites

•Google search results

@imalittletester

Page 5: Selenium tests, the Object Oriented way

The ‘classic’ approach

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

•Code is heavy•Updating code is tricky

@imalittletester

Page 6: Selenium tests, the Object Oriented way

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

Page 7: Selenium tests, the Object Oriented way

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

Page 8: Selenium tests, the Object Oriented way

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

Page 9: Selenium tests, the Object Oriented way

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

Page 10: Selenium tests, the Object Oriented way

The Link

@imalittletester

Page 11: Selenium tests, the Object Oriented way

The Link

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

target="_blank">Read about testing

</a>

@imalittletester

Page 12: Selenium tests, the Object Oriented way

The Link object

@imalittletester

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

private String linkTarget;

private String linkLabel;

Page 13: Selenium tests, the Object Oriented way

@imalittletester

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

this.linkUrl = linkUrl;

this.linkTarget = linkTarget;

this.linkLabel = linkLabel;}

Page 14: Selenium tests, the Object Oriented way

@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() }

Page 15: Selenium tests, the Object Oriented way

The test

@imalittletester

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

Page 16: Selenium tests, the Object Oriented way

The test

@imalittletester

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

new Link(page.testBlogLinkWebElement), expectedTestingBlogLink);

}

Page 17: Selenium tests, the Object Oriented way

The ReadingModule

@imalittletester

Page 18: Selenium tests, the Object Oriented way

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

Page 19: Selenium tests, the Object Oriented way

The ReadingModule object

@imalittletester

private Image image;

private String h2Text;

private Link testingBlogLink;

Page 20: Selenium tests, the Object Oriented way

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;}

Page 21: Selenium tests, the Object Oriented way

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")));}

Page 22: Selenium tests, the Object Oriented way

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);

Page 23: Selenium tests, the Object Oriented way

The ReadingModule test

@imalittletester

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

new ReadingModule(page.readingModuleDiv), expectedReadingModule);

}

Page 24: Selenium tests, the Object Oriented way

IDE results

• When assert fails:

@imalittletester

Page 25: Selenium tests, the Object Oriented way
Page 26: Selenium tests, the Object Oriented way

Summary

• Instead of writing a bunch of assertions, perform

object analysis

• Identify and model your objects

•Compare objects

@imalittletester

Page 27: Selenium tests, the Object Oriented way

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

Page 28: Selenium tests, the Object Oriented way

The CONs

•Failure console output difficult to read in case of

large objects

•Not following good naming conventions

confusion when reading object properties

@imalittletester

Page 29: Selenium tests, the Object Oriented way

@imalittletester

Got some questions?

Thank you!