Selenium tests, the Object Oriented way. The workshop · Selenium tests, the Object Oriented way....

43
Selenium tests, the Object Oriented way. The workshop Corina Pip @imalittletester https://iamalittletester.wordpress.com/

Transcript of Selenium tests, the Object Oriented way. The workshop · Selenium tests, the Object Oriented way....

Page 1: Selenium tests, the Object Oriented way. The workshop · Selenium tests, the Object Oriented way. The workshop Corina Pip @imalittletester

Selenium tests, the Object Oriented way.The workshop

Corina Pip

@imalittletester

https://iamalittletester.wordpress.com/

Page 2: Selenium tests, the Object Oriented way. The workshop · Selenium tests, the Object Oriented way. The workshop Corina Pip @imalittletester

• 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. The workshop · Selenium tests, the Object Oriented way. The workshop Corina Pip @imalittletester

The task

Page 4: Selenium tests, the Object Oriented way. The workshop · Selenium tests, the Object Oriented way. The workshop Corina Pip @imalittletester

- Navigate to https://iamalittletester.wordpress.com/

- Look at the ‘featured’ content section

- Test that the ‘featured’ content is the one from the image

below

- Do this task by using only ONE assert

Page 5: Selenium tests, the Object Oriented way. The workshop · Selenium tests, the Object Oriented way. The workshop Corina Pip @imalittletester

Phase 1: Analysis

Page 6: Selenium tests, the Object Oriented way. The workshop · Selenium tests, the Object Oriented way. The workshop Corina Pip @imalittletester

Phase 1: Analysis

• Structure of module:• 3 articles

• Each article:• 1 clickable image with link

• 3 assigned tags which are links

• 1 link to the post

Page 7: Selenium tests, the Object Oriented way. The workshop · Selenium tests, the Object Oriented way. The workshop Corina Pip @imalittletester

• 3 articles

Page 8: Selenium tests, the Object Oriented way. The workshop · Selenium tests, the Object Oriented way. The workshop Corina Pip @imalittletester

• Each article:• 1 clickable image with link

<article>

<a href=…>

<img width=… height=… src=… />

</a>

...

</article>

Page 9: Selenium tests, the Object Oriented way. The workshop · Selenium tests, the Object Oriented way. The workshop Corina Pip @imalittletester

• Each article:• 3 assigned tags which are links

<article>

...

<a href=… rel="category tag"> Label </a>

<a href=… rel="category tag"> Label </a>

<a href=… rel="category tag"> Label </a>

...

</article>

Page 10: Selenium tests, the Object Oriented way. The workshop · Selenium tests, the Object Oriented way. The workshop Corina Pip @imalittletester

• Each article:• 1 link to the post

<article>

...

<a href=… rel="bookmark"> Label </a>

</article>

Page 11: Selenium tests, the Object Oriented way. The workshop · Selenium tests, the Object Oriented way. The workshop Corina Pip @imalittletester

• So, for each article:

<article><a href=…>

<img width=… height=… src=… /></a>

…<a href=… rel="category tag"> Label </a><a href=… rel="category tag"> Label </a><a href=… rel="category tag"> Label </a>

... <a href=… rel="bookmark"> Label </a>

</article>

Page 12: Selenium tests, the Object Oriented way. The workshop · Selenium tests, the Object Oriented way. The workshop Corina Pip @imalittletester

Phase 2: Defining the objects

Page 13: Selenium tests, the Object Oriented way. The workshop · Selenium tests, the Object Oriented way. The workshop Corina Pip @imalittletester

The featured content module

• Will be an object with 3 properties:• Featured content 1

• ClickableImage: href, width, height, src• TagSection

• Link1: href, label

• Link2: href, label

• Link3: href, label

• Link: href, label

• Featured content 2: same properties as first one

• Featured content 3: same properties as first one

Page 14: Selenium tests, the Object Oriented way. The workshop · Selenium tests, the Object Oriented way. The workshop Corina Pip @imalittletester

Each object consists of

•Properties•Constructor for expected content•Constructor for actual content•equals() and hashCode() methods – for comparing objects•toString() method – to print object to the console

Page 15: Selenium tests, the Object Oriented way. The workshop · Selenium tests, the Object Oriented way. The workshop Corina Pip @imalittletester

An image object: ClickableImage

•Will be named: ClickableImage

•Will be an object with 4 properties: href, src, width, height

•Will have two constructors

•Will have the additional methods: equals(), hashCode(), toString()

Page 16: Selenium tests, the Object Oriented way. The workshop · Selenium tests, the Object Oriented way. The workshop Corina Pip @imalittletester

ClickableImage

• Will be an object with 4 properties: href, src, width, height

public class ClickableImage {

private String imageHref;

private String imageWidth;

private String imageHeight;

private String imageSource;

Page 17: Selenium tests, the Object Oriented way. The workshop · Selenium tests, the Object Oriented way. The workshop Corina Pip @imalittletester

Generating constructors

- inside the class, click Alt + Ins:

- then, select all the displayed properties

Page 18: Selenium tests, the Object Oriented way. The workshop · Selenium tests, the Object Oriented way. The workshop Corina Pip @imalittletester

ClickableImage – constructor for EXPECTED

public ClickableImage(String imageHref, String imageWidth, String

imageHeight, String imageSource) {

this.imageHref = imageHref;

this.imageWidth = imageWidth;

this.imageHeight = imageHeight;

this.imageSource = imageSource;

}

Page 19: Selenium tests, the Object Oriented way. The workshop · Selenium tests, the Object Oriented way. The workshop Corina Pip @imalittletester

ClickableImage – constructor for ACTUAL

public ClickableImage(WebElement element) {

this.imageHref = element.getAttribute("href");

this.imageWidth =

element.findElement(By.cssSelector("img")).getAttribute("width");

this.imageHeight =

element.findElement(By.cssSelector("img")).getAttribute("height");

this.imageSource =

element.findElement(By.cssSelector("img")).getAttribute("src");

}

Page 20: Selenium tests, the Object Oriented way. The workshop · Selenium tests, the Object Oriented way. The workshop Corina Pip @imalittletester

equals(), hashCode(), toString()

•Click Alt + Ins yet again

•Choose: equals() and hashCode()

•Click Alt + Ins yet again

•Choose: toString()

Page 21: Selenium tests, the Object Oriented way. The workshop · Selenium tests, the Object Oriented way. The workshop Corina Pip @imalittletester

Link

• Each item in the tag section is an object of type Link

•A Link object has two properties: href, label

public class Link {

private String href;

private String label;

Page 22: Selenium tests, the Object Oriented way. The workshop · Selenium tests, the Object Oriented way. The workshop Corina Pip @imalittletester

Link – constructor for EXPECTED

public Link(String href, String

label) {

this.href = href;

this.label = label;

}

Page 23: Selenium tests, the Object Oriented way. The workshop · Selenium tests, the Object Oriented way. The workshop Corina Pip @imalittletester

Link - constructor for ACTUAL

public Link(WebElement element) {

this.href

= element.getAttribute("href

");

this.label = element.getText();

}

+ equals(), hashCode(), toString()

Page 24: Selenium tests, the Object Oriented way. The workshop · Selenium tests, the Object Oriented way. The workshop Corina Pip @imalittletester

TagSection

•An Object with 3 properties of type Link

public class TagSection {

private Link tag1;

private Link tag2;

private Link tag3;

Page 25: Selenium tests, the Object Oriented way. The workshop · Selenium tests, the Object Oriented way. The workshop Corina Pip @imalittletester

TagSection – constructor for EXPECTED

public TagSection(Link tag1, Link tag2, Link

tag3) {

this.tag1 = tag1;

this.tag2 = tag2;

this.tag3 = tag3;

}

+equals(), hashCode(), toString()

Page 26: Selenium tests, the Object Oriented way. The workshop · Selenium tests, the Object Oriented way. The workshop Corina Pip @imalittletester

TagSection – constructor for ACTUAL

public TagSection(List<WebElement> elementList)

{

this.tag1 = new Link(elementList.get(0));

this.tag2 = new Link(elementList.get(1));

this.tag3 = new Link(elementList.get(2));

}

Page 27: Selenium tests, the Object Oriented way. The workshop · Selenium tests, the Object Oriented way. The workshop Corina Pip @imalittletester

Article

•One of the 3 items of the featured content = Article

public class Article {

private ClickableImage

clickableImage;

private TagSection tagSection;

private Link postLink;

Page 28: Selenium tests, the Object Oriented way. The workshop · Selenium tests, the Object Oriented way. The workshop Corina Pip @imalittletester

Article – constructor for EXPECTED

public Article(ClickableImage clickableImage,

TagSection tagSection, Link postLink) {

this.clickableImage = clickableImage;

this.tagSection = tagSection;

this.postLink = postLink;

}

Page 29: Selenium tests, the Object Oriented way. The workshop · Selenium tests, the Object Oriented way. The workshop Corina Pip @imalittletester

Article - constructor for ACTUAL

public Article(WebElement imageElement,

List<WebElement> tagElementList, WebElement

postElement) {

this.clickableImage = new

ClickableImage(imageElement);

this.tagSection = new TagSection(tagElementList);

this.postLink = new Link(postElement);

}

Page 30: Selenium tests, the Object Oriented way. The workshop · Selenium tests, the Object Oriented way. The workshop Corina Pip @imalittletester

Featured content section

• FeaturedContent Object – made up of three Article Objects

public class FeaturedContentSection {

private Article article1;

private Article article2;

private Article article3;

Page 31: Selenium tests, the Object Oriented way. The workshop · Selenium tests, the Object Oriented way. The workshop Corina Pip @imalittletester

Featured content section - EXPECTED

public FeaturedContentSection(Article article1,

Article article2, Article article3) {

this.article1 = article1;

this.article2 = article2;

this.article3 = article3;

}

Page 32: Selenium tests, the Object Oriented way. The workshop · Selenium tests, the Object Oriented way. The workshop Corina Pip @imalittletester

Featured content section - ACTUAL

public FeaturedContentSection(WebElement _1_imageElement, List<WebElement>

_1_tagElementList, WebElement _1_postElement,

WebElement _2_imageElement, List<WebElement>

_2_tagElementList, WebElement _2_postElement,

WebElement _3_imageElement, List<WebElement>

_3_tagElementList, WebElement _3_postElement) {

this.article1 = new Article(_1_imageElement, _1_tagElementList, _1_postElement);

this.article2 = new Article(_2_imageElement, _2_tagElementList, _2_postElement);

this.article3 = new Article(_3_imageElement, _3_tagElementList, _3_postElement);

}

Page 33: Selenium tests, the Object Oriented way. The workshop · Selenium tests, the Object Oriented way. The workshop Corina Pip @imalittletester

Phase 3: Define the PageObject

Page 34: Selenium tests, the Object Oriented way. The workshop · Selenium tests, the Object Oriented way. The workshop Corina Pip @imalittletester

Define the PageObject

• @FindBy(how = CSS, using = "#post-564 a") public WebElement image1Element;

@FindBy(how = CSS, using = "#post-564 header

a") public List<WebElement> tagList1Element;

@FindBy(how = CSS, using = "#post-564 h1 a")

public WebElement post1Element;

Page 35: Selenium tests, the Object Oriented way. The workshop · Selenium tests, the Object Oriented way. The workshop Corina Pip @imalittletester

Phase 4: Write the tests

Page 36: Selenium tests, the Object Oriented way. The workshop · Selenium tests, the Object Oriented way. The workshop Corina Pip @imalittletester

Generate expected content

private ClickableImage image1 = new ClickableImage("https://iamalittletester.wordpress.com/2016/02/02/ho

w-to-identify-the-test-scenarios-you-have-to-automate/", "346","192", "https://iamalittletester.files.wordpress.com/2016/01/24680224896_5c3e7335c5_o.jpg?w=672&h=372&crop=1");private ClickableImage image2 = new ClickableImage(

"https://iamalittletester.wordpress.com/2015/04/03/working-with-cookies-in-selenium/", "346", "192",

"https://iamalittletester.files.wordpress.com/2015/04/dsc_0882.jpg?w=672&h=372&crop=1");private ClickableImage image3 = new ClickableImage(

"https://iamalittletester.wordpress.com/2014/05/05/useful-generating-random-strings-with-randomstringutils/",

"346", "192", "https://iamalittletester.files.wordpress.com/2014/05/13643775275_e4d2dd2da3_o.jpg?w=672&h=372&crop=1");

Page 37: Selenium tests, the Object Oriented way. The workshop · Selenium tests, the Object Oriented way. The workshop Corina Pip @imalittletester

Generate expected content

private Link tag_automation = new Link("https://iamalittletester.wordpress.com/category/automation/", "AUTOMATION");private Link tag_code = new Link("https://iamalittletester.wordpress.com/category/code/", "CODE");private Link tag_testing = new Link("https://iamalittletester.wordpress.com/category/testing/", "TESTING");private Link tag_selenium = new Link("https://iamalittletester.wordpress.com/category/selenium/", "SELENIUM");private Link tag_flavours = new Link("https://iamalittletester.wordpress.com/category/flavours/", "FLAVOURS");

Page 38: Selenium tests, the Object Oriented way. The workshop · Selenium tests, the Object Oriented way. The workshop Corina Pip @imalittletester

Generate expected content

//below part of the article - the link to the postprivate Link postLink1 = new Link("https://iamalittletester.wordpress.com/2016/02/02/how-to-identify-the-test-scenarios-you-have-to-automate/",

"HOW TO IDENTIFY THE TEST SCENARIOS YOU HAVE TO AUTOMATE");private Link postLink2 = new Link("https://iamalittletester.wordpress.com/2015/04/03/working-with-cookies-in-selenium/",

"WORKING WITH COOKIES IN SELENIUM");private Link postLink3 = new Link("https://iamalittletester.wordpress.com/2014/05/05/useful-generating-random-strings-with-randomstringutils/",

"USEFUL: GENERATING RANDOM STRINGS WITH RANDOMSTRINGUTILS");

Page 39: Selenium tests, the Object Oriented way. The workshop · Selenium tests, the Object Oriented way. The workshop Corina Pip @imalittletester

Generate expected content

//tag sectionsprivate TagSection tagSection1 = new TagSection(tag_automation, tag_code, tag_testing);private TagSection tagSection2 = new TagSection(tag_automation, tag_selenium, tag_testing);private TagSection tagSection3 = new TagSection(tag_automation, tag_flavours, tag_testing);

//articlesprivate Article article1 = new Article(image1, tagSection1, postLink1);private Article article2 = new Article(image2, tagSection2, postLink2);private Article article3 = new Article(image3, tagSection3, postLink3);

//the featured content sectionprivate FeaturedContentSection featuredContentSection = new FeaturedContentSection(article1, article2, article3);

Page 40: Selenium tests, the Object Oriented way. The workshop · Selenium tests, the Object Oriented way. The workshop Corina Pip @imalittletester

The test

@Testpublic void theTest() {

webDriver.get("https://iamalittletester.wordpress.com/";

assertEquals(new FeaturedContentSection(page.image1Element, page.tagList1Element, page.post1Element,

page.image2Element, page.tagList2Element, page.post2Element,

page.image3Element, page.tagList3Element, page.post3Element), featuredContentSection);}

Page 41: Selenium tests, the Object Oriented way. The workshop · Selenium tests, the Object Oriented way. The workshop Corina Pip @imalittletester

Summary

• Analyze the module under test

• Map module and properties to objects and object properties

• Create constructors for expected and actual content

• Add equals(), hashCode(), toString() methods to objects

• Create page objects

• Generate expected and actual content objects

• Compare expected and actual objects

Page 42: Selenium tests, the Object Oriented way. The workshop · Selenium tests, the Object Oriented way. The workshop Corina Pip @imalittletester

Possible improvements

•Reduce constructor signatures

• Extract common texts into variables/constants

•Add more constructors if needed

•Override display for assertion error

Page 43: Selenium tests, the Object Oriented way. The workshop · Selenium tests, the Object Oriented way. The workshop Corina Pip @imalittletester

Solution available

• Git project: https://github.com/iamalittletester/learning-

project

• Page objects: https://github.com/iamalittletester/learning-

project/tree/master/src/main/java/pageobjects/seOO/ntd17

• Objects + test : https://github.com/iamalittletester/learning-

project/tree/master/src/test/java/ntd17