Showing posts with label Quality Assurance. Show all posts
Showing posts with label Quality Assurance. Show all posts

Friday, 7 July 2017

Selenium WebDriver : Working with Checkboxes

In this post we are going to learn about automating the interaction with Checkboxes. Checkbox is an HTML Web Page element which allows users to select one or more options that are listed on a Web Page. Users just need to select the required option from the list, so we just need to click the Checkbox Option through our Automation script to select one of the Checkboxes.

Don't forget to subscribe to our YouTube Channel to see how to Automate and select a checkbox :

 YouTube Channel : Click Here



HTML Source Code for checkboxes looks like :

<input id="checkBox1" type="checkbox">
<input id="checkBox2" type="checkbox">
<input id="checkBox3" type="checkbox">  



We need to find the required checkbox(s) and click on it. Sounds simple right ?
So let's see how that can be done :


public class Sample {
   public static void main(String[] args) {
      //Initialize Browser
      System.setProperty("webdriver.gecko.driver", "Path to geckodriver.exe");
      WebDriver driver = new FirefoxDriver();
      driver.manage().window().maximize(); 
      driver.get("Web Page URL");
 
      //Find Element and Click
      WebElement element = driver.findElement("Checkbox locator");
      element.click();
   }
}



element.click() will click on the required checkbox option.

So that's the way we can click on the required checkbox option through our Automation script.

Feel free to comment in the comments section in case you have any queries.
Happy Learning.

Don't forget to Subscribe :
YouTube Channel : Click Here

Friday, 9 June 2017

Selenium WebDriver : Getting text from Web Page

If you want to get some text (full / partial) that is displayed on Web Page, for verification, data extraction or any other purpose, we have some WebDriver methods that will do the job for us. Users with basic Object-Oriented knowledge can easily use these methods to achieve what is required.

In Types of Locators, we have seen different techniques to locate Web Page elements that are required to be found during Test Automation. It includes finding by ID, ClassName, Tag, LinkText, etc. that can be achieved by using appropriate Selenium WebDriver methods using the WebDriver Object instance.

In this post, we are going to see the way of extracting text from Web Page using Selenium WebDriver. Trust me it is as simple as one line of code.
Check the following code snippet :



public class Sample {
public static void main(String[] args) {
//Initialize Browser
System.setProperty("webdriver.gecko.driver", "E:\\AllJARS\\Selenium_Driver_Servers\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("https://www.google.com/");

//Find element
WebElement element = driver.findElement(By.cssSelector("a[href='https://mail.google.com/mail/?tab=wm']"));

//Get Text
System.out.println(element.getText());
}
}


The above code snippet navigates to Google, finds link (Gmail), extracts the text (Gmail) and prints it on console.

getText() method is used to extract the text from the Web Page element found using one of the locating techniques.
In this way we can find the required element and get or extract the required text from the Web Page. Further, we can use any substring() method provided by Java to get the desired String value (reduced length compared to Original String).


element.getText();

So, the above line of code can be used to extract the text from Web Page as required. Simple, isn't it ?
Hope you liked this post. In case of any queries or suggestions, feel free to comment in comments section.
Thank you for reading.

Happy Learning.

YouTube Channel : Click Here

Wednesday, 25 November 2015

Selenium WebDriver : Basic User Actions

Let us have a look at some basic user actions such as :


  1. Open Browser
  2. Open URL
  3. Sending data to Text Field
  4. Mouse Click
  5. Select Drop-down Option




Open Browser :

This is the first step towards writing Test Case Automation Script (for Web Applications). There are different ways to Open different browsers programmatically. Here we will discuss about three browsers : Mozilla Firefox, Internet Explorer, Google Chrome.

We need to download required Driver Servers before opening a Web Browser in Selenium WebDriver Script :


To download Driver Servers of other browsers, click here



Mozilla Firefox

If Selenium JARS version < 3.0 :

WebDriver driver = new FirefoxDriver();


If Selenium JARS version >= 3.0 :

File geckoDriver = new File("path to \\geckoDriverServer.exe"); System.setProperty("webdriver.gecko.driver", geckoDriver.getAbsolutePath() );
WebDriver driver = new FirefoxDriver();

 

Internet Explorer

File IEDriver = new File("path to \\IEDriverServer.exe");
System.setProperty("webdriver.ie.driver", IEDriver.getAbsolutePath() );
WebDriver driver = new InternetExplorerDriver();



Google Chrome
 
System.setProperty("webdriver.chrome.driver", "path to \\chromedriver.exe");
WebDriver driver = new ChromeDriver();




WebDriver is the interface included in the Selenium JARs package. It contains various methods to perform different user actions.





Open URL :

Following line of code can be used to open a URL in any browser mentioned above :

driver.get(URL String);

Example :
driver.get("https://www.google.co.in/");

This opens the required URL (or Website) in the browser initiated programmatically.






Sending data to Text Field :

This is one of the most frequently used User Action during Automation Script development. It is used while logging into a site, filling form data, etc.
Following line of code is used to fill data in a Text Field :

driver.findElement(By.cssSelector(locator)).sendKeys(data);

locator - This is the locator used to locate Web Element using one of the locating methods.
data - This is the String that we want to pass to the Text Field.






Mouse Click :

This is another frequently used User Action used to log in, Save form data, select drop-down option, etc.
Following line of code is used for a Mouse Click :

driver.findElement(By.cssSelector(locator)).click();

Here locator can be any Web Element such as a button, drop-down option or any element that a user can click.






Select Drop-down Option :

Selenium provides us with some inbuilt functions to select an option from drop-down menu.

Select option = new Select(driver.findElement(By.id(locator)));
option.selectByVisibleText(Drop down Text);

                         or
option.selectByIndex(Index Value);
                         or
option.selectByValue(Value Attribute)


Example : Let us consider this HTML code snippet

<html>
<body>
<select id = "designation">
          <option value = "MD">MD</option>
          <option value = "prog"> Programmer </option>
          <option value = "CEO"> CEO </option>
</option>
</select>
<body>
</html>


To select Programmer, we can use any of the below methods :

Select option = new Select(driver.findElement(By.id(locator)));
option.selectByVisibleText("Programmer");
                    or
option.selectByIndex(1);
                    or
option.selectByValue("prog")


 


These are some of the User Actions used during Automation Script Development.


We will discuss about some advanced User Actions in upcoming posts.
Happy Learning.

YouTube Channel : Click Here

Tuesday, 24 November 2015

Getting Started - Selenium Project Set up in Eclipse

In this post, we will discuss about how to set up a Java Project in Eclipse and include required Selenium JAR files to develop Automation Test Cases.

For those who are not familiar with Eclipse, Click here


To create Project in Eclipse :

  • Click on File > New > Java Project.
    Enter "Project Name" and click on "Finish".


  • Configure the Project to include Selenium JAR files.
    Right Click on Project created > Build Path > Configure Build Path

  • Make sure that you have downloaded the latest JAR files (Download latest Selenium JAR files from here. I recommend to use latest Selenium JARS version with your updated browsers)
    Click on "Add External JARs" > Browse and select all JAR files > Click on "OK"



After following above mentioned steps, we are ready with required minimum configuration to use predefined Classes and Methods required for developing Automation Test Cases. There are several other JAR files required to perform different actions such as reading Excel / CSV file, generating custom reports, etc, which are not included in downloaded Selenium JARs package.
We will cover such scenarios one-by-one in upcoming posts.

We will discuss about automating basic user actions such as opening a browser, opening a URL, identifying and performing required actions with web elements, etc in the upcoming posts.
Happy Learning.

YouTube Channel : Click Here

Thursday, 11 June 2015

Selenium - Types Of Locators

In this post, lets discuss about various types of Locators available to locate Web Page Elements.
This is the most crucial part of automation since we start automating after locating required elements on the Web Page !!

Before trying out any of the examples below, make sure that you have JAVA and Eclipse installed on your machine.
Also download Selenium JARS (Click here)

Different Types of Locators include :


  • ID
  • Class Name
  • CSS
  • LinkText
  • Tag Name
  • Xpath
  • Name

These are the different methods used to locate Web Page elements.
Lets discuss each of them.






1 - Locating By ID :
          This is the first type of locator that we must use to locate an element (if ID of the required element exist). Since IDs are unique, we can find the required element easily by specifying its ID.
Example
          <input id = "email" class = "required" type = "text" />
WebElement element = driver.findElement(By.id("email"));

If there is no ID for some particular desired element than we have to use other methods of locating elements.






2 - Locating By Class Name :
          Since multiple elements can belong to the same class, we have to ensure that the required element is located when using this method of locating elements. This is mostly used in conjunction with CSS for locating the right element. By default, it will return the first match it finds on the page.
Example
          <input id = "email" class = "required" type = "text" />
WebElement element = driver.findElement(By.className("required"));

In this example, it finds the above tag but on actual sites you have to make sure that right element is found.






3 - Locating By CSS :
          This makes use of the DOM structure of the page and includes various combinations of other locators. Hence we will discuss about CSS method of locating elements in the next post.






4 - Locating By LinkText :
          This is used only when we want to find an element which is a link. We find it using the text of the link.
Example :
          <a href = "https://google.co.in"> Google </a>
Webelement element = driver.findElement(By.linkText("Google"));

This way we can find the link that is displayed with the required text that we want to find. The element is found only when the link with desired text is present on the page.






5 - Locating By Tag Name :
          Another way of locating element is by using the tag name of the element. But like class names, we can have the same tag appear multiple times on the page. So care must be taken while using this approach.
Example :
          <p> This is a para </p>
WebElement element = driver.findElement(By.tagName("p"));

Again, make sure that the right element is found.






6 - Locating By Name :
          This makes use of the name attribute of the element to be found.
Example :
          <div id="pancakes">
                <button type="button" name="pancake" value="Blueberry">Blueberry</button>
                <button type="button" name="pancake" value="Banana">Banana</button>
                <button type="button" name="pancake" value="Strawberry">Strawberry</button>
          </div>
WebElement element = driver.findElement(By.name("pancake"));






7 - Locating By Xpath :
          It is useful in case of cross browser testing since different browsers render CSS in different ways, hence using CSS selectors to locate elements would behave differently on different browsers.
To find Xpath - Right Click on the element and select "Copy Xpath".





To conclude, we have seen different types of locators that can be used to locate elements on a web page. The best way is to use ID or name of the element if they exist.

Will discuss about setting up of the project in Eclipse (integrating Selenium JARS) and CSS Selectors method of locating element in the upcoming post.
Happy Learning.

YouTube Channel : Click Here