Page Factory Design Pattern

In Selenium 'Page factory Design Pattern' is Extensive and Enhanced Version of Page Object Model(POM) , Page Factory pattern is used to initialize web elements which are defined in Page Objects.

How Elements are identified in Page Factory Design Pattern ?

@FindBy annotation is used to Identify Elements in Page Factory ,Annotations supports all 8 Type of Selenium Locators (id, name, className, css, xpath, tagName, linkText and partialLinkText)

There is two ways to use this annotation.

1. By 'How'

@FindBy(how = How.LocatorName, using = "Value")
private WebElement ElementName; Ex-
@FindBy(how = How.ID, using = "password") private WebElement Password;

2. By Direct Locator name @FindBy(LocatorName="Value") private WebElement ElementName; Ex-
@FindBy(id="password") private WebElement Password; For Multiple Elements Handlng(List of WebElements) Ex- @FindBy(tagName = "mylist") private List<WebElement> links;

How to Initialize WebElements Defined in Page Object ?

page objects Initalzation should be done using initElements() method from PageFactory Class as below, Once we call initElements() method, all elements will get initialized. PageFactory.initElements() static method takes the driver instance of the given class and the class type, and returns a Page Object with its fields fully initialized.

LoginPage loginpage = new LoginPage(driver);
PageFactory.initElements(driver, homePage);

Note : LoginPage is a class which have page objects .

// To initialize LoginPage Objects or Elements
LoginPage loginpage = PageFactory.initElements(driver, LoginPage.class);

// Using Constructor
public LoginPage(WebDriver driver) {
this.driver = driver;
PageFactory.initElements(driver, this);

How to Open a Link in New Tab in Selenium

In this Example, we will open a link in New tab, So below mentioned step you have to perform to achieve this :

1. Create an XPath or find a unique path of the link.

Ex : As Mentioned in ScreenShot we are going to open a 'CORE JAVA' Link in new Tab So we have taken a Unique Path.

Image 1

2. Get 'href ' Attribute from WebElement, because of Every Link Consist of a Unique URL

String UrlofLink = driver.findElement(By.xpath("//a[contains(text(),'Core Java')]")).getAttribute("href");
or you can Devide Script into Two Parts

WebElement Link = driver.findElement(By.xpath("//a[contains(text(),'Core Java')]"));                                                         String UrlofLink=Link.getAttribute("href");
3. To Open a New Tab you have to use JavaScript Executor and WindowHandler, JavaScriptExecutor has Used for Open a Blank new Tab and Through windowHandler, you will get a count of current Tab based on Count you can switch to parent tab to child Tab and Vice-versa.

public class OpenNewTab{
public static void main(String[] args) throws IOException {
 WebDriver driver=new ChromeDriver();
 String baseUrl = "";
 // Get a Url from Link 
 WebElement Link = driver.findElement(By.xpath("//a[contains(text(),'Core Java')]"));                                                         
 String UrlofLink=Link.getAttribute("href");
   // To open a New tab 
 //Get a Count of Current open Window
    ArrayList<String> tabs = new ArrayList<String> (driver.getWindowHandles());
 //switches to new tab

    // To open a link in New Tab
 // switch back to main screen 

Locators in Selenium


Locators provide a mechanism to access the HTML elements on a web page. Once we locate an element/object on any web page, Then to get or store the Webelement we use findElement() or Findelements()  method to find or strore the located webelement or List of elements then we can write test cases to execute on them.

Selenium uses what is called locators to find and match the elements of your page that it needs to interact with

                           For example, If we would like click on any button then locators will help me to click on button , We will see that in details in the subsequent sections of this tutorial.

Types of Locators in Selenium 

Selenium webdriver Have 8 locators to find the Web elements on web page , We have mentioned the list of locators as per priority to be used while Coding.

  • ID
  • Name 
  • Link Text
  • Partial Link Text
  • Tag Name 
  • Class Name
  • Css Selector
  • XPATH   - Absolute and Relative 


Configuration of Selenium WebDriver Project With Java part 2

In Last Article we have seen that what different stuff required for Maven Project and Java Project Configuration  ,In this article we will learn about final configuration of Selenium WebDriver Project

  • Create a Java Project or Maven Project if You want to work on Existing Project then Skip this Step ,otherwise i prefer to create Setup in New Project ,if You do not How To create Simple Java Project Click Here
  • Right click on  Java/Maven Project  >New >Folder > Enter Folder Name Like -'Jar' >Click on Finish
  • For Java Project -Copy Selenium Standalone jar which you have downloaded in First step and Paste Inside Folder 'Jar'  > Right Click on Selenium Standalone jar >Go To Build Path > Click On Add To Build Path ,Maven Project user should skip this Step 
  • For Maven Project Copy Dependency from Previous Article and Paste inside POM.XML Dependencies Tag 
  • Right click on  Java/Maven Project  >New >Folder > Enter Folder Name Like -'Exe' >Click on Finish
  • Copy ChromeDriver.exe File which you have downloaded in First step and Paste Inside Folder 'Exe' for Chrome Browser ,you can use GeckoDriver for Firefox ,IEDriver for Internet Explore and So on 
                          Now your Selenium Configuration beginners phase have been completed now you can Start to Automate to any Web Application .

 Previous Article                                                                                                       Continue Reading ...

Configuration of Selenium WebDriver Project With Java part 1

To install Selenium means to set up a project in a development so you can write a program using Selenium. How you do this depends on your programming language and your development environment and Selenium is Language Independent Tool So You can use any one Programming Language among -  Java , C# , Python , Ruby , PHP , JavaScript , Perl .

The easiest way to set up a Selenium 2.0 Java project is to use Maven. Maven will download the java bindings (the Selenium 2.0 java client library) and all its dependencies, and will create the project for you, using a maven pom.xml (project configuration) file. Once you’ve done this, you can import the maven project into your preferred IDE, IntelliJ IDEA or Eclipse.
Maven Project
 For Maven Project you Should have Selenium Dependency in POM.xml ,Selenium Dependency you can get from Here .

<!-- -->
Copy this Dependency and Paste inside a  <Dependencies>  Paste Selenium Dependency  </Dependencies> Tag 
                  Be sure you specify the most current version. At the time of writing, the version listed above was the most current, however there were frequent releases immediately after the release of Selenium 2.0. Check the Maven download page for the current release and edit the above dependency accordingly.

Now, from a command-line, CD into the project directory and run maven as follows.

mvn clean install
     Java Project  

For those user who is using Simple Java Project they can Download Jar Selenium Latest Jar

Previous Article                                                                                                       Continue Reading ...

Introducing Selenium WebDriver

The primary new feature in Selenium Webdriver 2.0 is the integration of the WebDriver API. WebDriver is designed to provide a simpler, more concise programming interface in addition to addressing some limitations in the Selenium-RC API. Selenium-WebDriver was developed to better support dynamic web pages where elements of a page may change without the page itself being reloaded. WebDriver’s goal is to supply a well-designed object-oriented API that provides improved support for modern advanced web-app testing problems.

WebDriver Compared to Selenium-RC

Selenium-WebDriver makes direct calls to the browser using each browser’s native support for automation. How these direct calls are made, and the features they support depends on the browser you are using. Information on each ‘browser driver’ is provided later in this chapter.

For those familiar with Selenium-RC, this is quite different from what you are used to. Selenium-RC worked the same way for each supported browser. It ‘injected’ javascript functions into the browser when the browser was loaded and then used its javascript to drive the AUT within the browser. WebDriver does not use this technique. Again, it drives the browser directly using the browser’s built in support for automation.

Previous Article                                                                                                        Continue Reading ...

WebDriver and the Selenium-Server

You may, or may not, need the Selenium Server, depending on how you intend to use Selenium-WebDriver. If your browser and tests will all run on the same machine, and your tests only use the WebDriver API, then you do not need to run the Selenium-Server; WebDriver will run the browser directly.

There are some reasons though to use the Selenium-Server with Selenium-WebDriver.

  • You are using Selenium-Grid to distribute your tests over multiple machines or virtual machines (VMs).
  • You want to connect to a remote machine that has a particular browser version that is not on your current machine.
  • You are not using the Java bindings (i.e. Python, C#, or Ruby) and would like to use HtmlUnit Driver