Hello Folks,
You must have seen a web page which contains many divisions or multiple web elements and similar web elements in multiple divisions. Creating a web element repository for such pages is difficult in some cases which are given below:
We can resolve above issues using inner class concept of Java. We can divide each section as a static inner class. It will also initialise only particular section web elements instead of all web elements of page.
Sample code:
package PageObjectModelWithInnerClass; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.support.FindBy; import org.openqa.selenium.support.PageFactory; public class Page_Login { // Create an account section public static class createAnAccount { // Initializing only createAnAccount members public static createAnAccount getcreateAnAccount(WebDriver driver) { return PageFactory.initElements(driver, createAnAccount.class); } @FindBy(id="email_create") public WebElement txt_emailAddress; @FindBy(id="SubmitCreate") public WebElement btn_createAccount; @FindBy(xpath="//h3[text()='Create an account']") public WebElement lbl_createAnAccount; @FindBy(xpath="//p[text()='Please enter your email address to create an account.']") public WebElement lbl_createAnAccountDesc; } // Already registered section public static class alreadyRegistered { // Initializing only createAnAccount members public static alreadyRegistered getalreadyRegistered(WebDriver driver) { return PageFactory.initElements(driver, alreadyRegistered.class); } @FindBy(id="email") public static WebElement txt_emailAddress; @FindBy(id="passwd") public static WebElement txt_password; @FindBy(id="SubmitLogin") public static WebElement btn_login; @FindBy(xpath="//h3[text()='Create an account']//a[text()='Forgot your password?']") public static WebElement lnk_forgotPassword; } }
In Test script class, you just need to access required inner class as below:
package PageObjectModelWithInnerClass; import java.util.concurrent.TimeUnit; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; public class CreateAnAccount { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver","./exefiles/chromedriver.exe"); WebDriver driver= new ChromeDriver(); driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); driver.get("http://automationpractice.com/index.php?controller=authentication&back=my-account"); // Access inner class directly by its name as I have declared it as static Page_Login.createAnAccount.getcreateAnAccount(driver).txt_emailAddress.sendKeys("mse@gmail.com"); } }
Hope this will be helpful for you to create an object repository for a page which has multiple divisions and web elements.
If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.
#ThanksForReading
#HappySelenium
My name is Amod Mahajan and I am an IT employee with 4+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went throw so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning
Hello Folks, As part of our API Testing series, we will see "Tools to test SOAP and REST APIs manually and…
Hello Folks, As part of our API Testing series, we will see "Difference between SOAP and REST web services" in…
Hello Folks, As part of our API Testing series, we will see “Introduction of SOAP” in this post. SOAP stands…
Hello Folks, Most of us we know that to handle a dropdown developed using Select tag, we can use inbuilt…
Hello Folks, In this post we will going to learn an advanced concept of xpath: - normalize-space method. Before we discuss…
Hello Guys, Now we have good understanding of DataProviders in TestNG from previous articles. If you have not read my…