In
previous post, we had discussed about the challenges. Let’s 1st look
onto how can we share same driver object across pages?
The answer to the above question is, we can
implement Page Factory design patter in our project. In Page Factory Design
pattern, we have a method, PageFactory.initElements(driver,
LunchPage.class)
The above method will initialize the driver
object in LunchPage class internally.
Let’s
change the code as below & after that will explain the code overview:
Before
I explain the code overview, let’s discuss the s/w requirements of our project.
In this project, we are going to use Selenium 3. In selenium 3, to initialize
Firefox browser, we need to download the geckodriver.exe file & set the
system property value.
Download
geckodriver.exe file from here
& copy the .exe file into src/test/resources directory. For more details
please visit selenium official site to understand how selenium 3 works with
geckodriver.
In
SignInTest.java file replace the
previous @Test codes with below lines of code:
@Test
public void signInTet(){
System.setProperty("webdriver.gecko.driver", "./src/test/resources/geckodriver.exe");//sets the path of geckodriver
WebDriver
driver = new FirefoxDriver(); //Instantiate a
blank firefox driver
PageFactory.initElements(driver, LunchPage.class);//passes the
driver object in LuchPage
LunchPage lunchPage = new LunchPage(driver); //creates the object of lunch page
lunchPage.openURL(); // calls the method
inside lunch page
lunchPage.gotoSignInPage(); // calls the method
inside lunch page
SignInPage
signInPage = new SignInPage(); //creates the
object for signin page
signInPage.doLogIn("abc123@test.com", "abc123"); //calls the method
inside signin page
}
Code
Overview:
System.setProperty("webdriver.gecko.driver","./src/test/resources/geckodriver.exe");
The above line will set the system
property value to open firefox using geckodriver.
WebDriver driver = new
FirefoxDriver();
This line will open a blank firefox
window.
PageFactory.initElements(driver, LunchPage.class);
This line uses PageFactory’s initElements
method which will take the driver instance & will share the same driver instance in LunchPage class.
In
SignInPage.java file replace the
code as below:
public class LunchPage {
WebDriver
driver;
public LunchPage(WebDriver driver){
this.driver = driver;
}
/**
* initializes the browser & creates the
instance of the browser.
*/
public void openURL(){
System.out.println("Opening the
URL");
driver.get("http://automationpractice.com/index.php");//Will navigate to
the site
}
/**
* Navigates to Sign In Page.
* @return objectOfSignInPage
*/
public SignInPage
gotoSignInPage(){
System.out.println("Inside sign
in method");
SignInPage
signInPage = new SignInPage();
return signInPage;
}
}
Code
Overview:
- In the 1st line we are declaring the object of WebDriver.
- Creating the constructor & inside the constructor we are assigning the global driver object with the local driver using this keyword.
- In openURL method, we are calling the driver.get()method to navigate to the site URL.
To implement page
objects, download the files from here
& follow the steps below:
- Extract the .zip file.
- Copy & replace SignInTest.java file inside com.demo.pom.testcases package.
- Copy & replace SignInPage.java & LunchPage.java files inside com.demo.pom.pages package.
- In LunchPage.jave, I have declared the xpath of sign in link.
- In gotoSignInPage, added the webdriver code to click on SignIn link.
- SignInPage.java, here I have added the Xpaths of web elements & also updated the doLogin() code where it will insert the details & clicks on Sign In button.
By
changing the above code, if you are executing the SignInTest.java file then it
should open a firefox browser & will navigate to the URL: http://automationpractice.com/index.php
If
you are not facing with any problems, then you are ready to go ahead &
implement the Page Objects.
Implementing Page Objects in LunchPage
& SignInPage:
The
Page Objects are nothing but the web elements in that page. For implementing
the Page Objects, we will use @FindBy annotation in each Page classes &
using @FindBy annotation we will declare the web elements.
Code
Overview:
- In LunchPage.jave, I have declared the xpath of sign in link.
- In gotoSignInPage, added the webdriver code to click on SignIn link.
- SignInPage.java, here I have added the Xpaths of web elements & also updated the doLogin() code where it will insert the details & clicks on Sign In button.
Once,
the above steps are done run SignTest.java file & verify whether it’s able
to Login Successfully or not. If Login is success & user is in My Account page,
then we are ready to go ahead for further implementations. If everything works fine,
then you see the result as below:
No comments:
Post a Comment