Which Framework to use?
How to write scripts, store objects in a effective way?:
Frameworks:
data-driven, keyword-driven & Page Object Pattern
Why POP?
- easy to maintain
- easy readability of scripts
- reduce of eliminate duplicacy
- re-usability of code
- reliability
Basics of Page Object Pattern
PageObjects Class
Create Class for each Page:
Homepage - all Objects belonging to homepage
Login - JavaClass(LoginPage)
to identify the objects on one page
public class MaerkischeScholle {
WebDriver driver;
//local driver
public MaerkischeScholle(WebDriver driver) {
this.driver = driver;
//driver from where parameter is passed into constructor, is passed to local driver
}
By textFlatAvailability = By.xpath("//*[@id='article-27']");
public WebElement TextFlat() {
return driver.findElement(textFlatAvailability);
}
}
Testcase Class
Create a Class for Testcases
eg. Login
call Login-Field from Loginpage-Class
than you can act on the Login-Field
or get a Text from a Page and print it
package testcases;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
import objectrepository.MaerkischeScholle;
public class CheckAvailability {
@Test
public void CheckText() {
System.setProperty("webdriver.gecko.driver", "//home//helloworld//Documents//Code//Drivers" +
"//geckodriver");
WebDriver driver = new FirefoxDriver();
driver.get("https://www.maerkische-scholle.de");
MaerkischeScholle ms = new MaerkischeScholle(driver);
String text = ms.TextFlat().getText();
System.out.println(text);
}
}
Driver
The Webdriver is initiated in the Testcase Class
System.setProperty("webdriver.gecko.driver", "//home//helloworld//Documents//Code//Drivers" +
"//geckodriver");
WebDriver driver = new FirefoxDriver();
Testcases Class (parameter for new object of PageObjectClass)
MaerkischeScholle ms = new MaerkischeScholle(driver);
and then passed to the PageObjects-Class as a parameter through the constructor.
-> PageObjectsclass
WebDriver driver;
//local driver
public MaerkischeScholle(WebDriver driver) {
this.driver = driver;
//driver from where parameter is passed into constructor, is passed to local driver
PageFactory.initElements(driver, this);
// to use the @FindBy Annotation
}
if you switch to another page, just create a new PageObjekt in the Testcases Class and pass the driver
HomePage hp = new HomePage(driver);
PageObjectFactory @FindBy
@FindBy(xpath= "//*[@id='article-27']")
WebElement textFlatAvailability;
public WebElement TextFlat() {
return textFlatAvailability;
}
Top comments (0)