Component objects instead of page objects in selenium -


one of popular patterns in testing based on selenium page object. unfortunately, need duplicate code if use is. consider following situation:

  • we use ui framework, common component, fancy table
  • table quite complicated, has filtering, sorting, searching
  • the table used on several pages in app

is there existing infrastructure create more granular component objects instead of page objects either in selenium or in thrid party lbirary?. mean, annotations, , related infrastructure?

appium mobile implementation of selenium webdriver has concept of widgets extension of pageobjects. there widget class allows 1 search relative element including in web browser. can check out in appium source test section. have in package io.appium.java_client.pagefactory_tests.widgets. supports findby annotation , webelement construct , pagefactory initialization.

so instead of using

@findby(.......) private webelement mytable; 

you can use

@findby(container of table....) private table mytable; 

table class can have relevant variables , methods.

part of pom.xml

<dependency>     <groupid>org.seleniumhq.selenium</groupid>     <artifactid>selenium-api</artifactid>     <version>2.53.1</version> </dependency> <dependency>     <groupid>io.appium</groupid>     <artifactid>java-client</artifactid>     <version>4.1.2</version> </dependency> 

test class --

import io.appium.java_client.pagefactory.appiumfielddecorator;  import org.junit.test; import org.openqa.selenium.webdriver; import org.openqa.selenium.chrome.chromedriver; import org.openqa.selenium.support.pagefactory;  public class widgetbrowtest {      @test     public void test() {         system.setproperty("webdriver.chrome.driver", "e:/software testing/selenium/jars/chromedriver.exe");          webdriver driver = new chromedriver();          driver.get("http://stackoverflow.com/");           sohome sohome = new sohome(driver);         pagefactory.initelements(new appiumfielddecorator(driver), sohome);          //below 2 widget - first question in stackoverflow homepage         system.out.println(sohome.getfirstques().getquestitle());         system.out.println(sohome.getfirstques().getquestags());          //below 2 home page         system.out.println(sohome.getlogotext());         system.out.println(sohome.getmenutext());     } } 

stackoverflow home -

import java.util.list; import java.util.stream.collectors;  import org.openqa.selenium.webdriver; import org.openqa.selenium.webelement; import org.openqa.selenium.support.findby;  public class sohome {      @findby(css="div[id='hlogo'] > a")     private webelement logo;      @findby(xpath="//div[@id='hmenus']//li/a")     private list<webelement> menuopt;      @findby(css="div[class='summary']")     private soqueswidget firstques;      private webdriver driver;       public sohome(webdriver driver) {         this.driver = driver;     }      public string getlogotext() {         return logo.gettext();     }      public list<string> getmenutext() {         return menuopt.stream().map(t -> t.gettext()).collect(collectors.tolist());     }      public soqueswidget getfirstques() {         return firstques;     } } 

question widget - first question

import java.util.list; import java.util.stream.collectors;  import org.openqa.selenium.webelement; import org.openqa.selenium.support.findby;  import io.appium.java_client.pagefactory.widget;  public class soqueswidget extends widget {      @findby(css="a[class='question-hyperlink']")     private webelement questitle;      @findby(xpath=".//div[starts-with(@class,'tags')]/a")     private list<webelement> questags;      protected soqueswidget(webelement element) {         super(element);     }      public string getquestitle() {         return questitle.gettext();     }      public list<string> getquestags() {         return questags.stream().map(t -> t.gettext()).collect(collectors.tolist());     } } 

Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -