Showing posts with label test automation. Show all posts
Showing posts with label test automation. 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, 30 June 2017

Selenium WebDriver : Working with Drop-down Options

So, by now you must have learnt about how to open a Web Page, Navigate to another page by clicking on a link, click a button or verify text present on the Web Page. If not, you need to practice these basic User Actions that can be performed through Automation. You can also check the videos which explains about the different things that we have already discussed and will be discussing in the upcoming posts, related to Test Automation using Selenium WebDriver.

 YouTube Channel : Click Here

In this post we are going to learn yet another important feature of Selenium WebDriver, that is Selecting one of the Drop-down Options.

Before we see how to use the methods provided by Selenium WebDriver to select Drop-down Options, we need to initialize the Object which we will be using to Select options. Following line of code will find the <select> tag in HTML source code, and assign it to an object which we can use to call different methods :



Select option = new Select(driver.findElement("<select> tag locator"));

Once we have initialized the Object, we are ready to use the Object to call different methods.


There are different ways of selecting Drop-down option, let's see them one by one :


1 - Select By Visible Text :

You can select options based on the values displayed in the drop-down list.


option.selectByVisibleText("Option text String");

This will select the option based on the String passed as parameter to above mentioned method.


2 - Select by Value :


option.selectByValue("Option value attribute");

You can find the value attribute of the <option> tag in the HTML source code.

  

3 - Select by Index :


option.selectByIndex(index);

index is 0-based (starts from zero), to be incremented by one for next option.



In this way we can Select any of the Drop-down options using any of the methods provided by Selenium WebDriver.

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, 16 June 2017

Selenium WebDriver : Working with Radio Buttons

In this post, we are going to see how one can interact with Radio buttons present on a Web Page and select one of them. Radio Buttons are nothing but a list of options in which user is allowed to select exactly ONE option.

We are going to see how it can be done using Selenium WebDriver. Very simple, just stick to this post till the end. Also we have our YouTube Channel that will cover all the topics we are covering in this blog. So don't forget to subscribe to the channel for more interesting Test Automation videos. 

YouTube Channel : Click Here

So these are the HTML Radio Buttons as shown in below image :




HTML code looks like this :


  <input type="radio" name="animal" value="Bird"> Bird<br>
  <input type="radio" name="animal" value="Cat"> Cat<br>
  <input type="radio" name="animal" value="Dog"> Dog<br>
  <input type="radio" name="animal" value="Rabbit"> Rabbit<br>
  <input type="radio" name="animal" value="Pig"> Pig<br>




Now in order to select one of the Radio Button options, we just need to locate the appropriate option (Web Page Element) and click on that option using the click() method we already discussed in previous posts.



So in order to select Bird in above example, check the following code snippet : (Assuming the Web Page contains above HTML code)



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(By.cssSelector("input[value='Bird']"));
element.click();
}
}



driver.findElement(By.cssSelector("input[value='Bird']")); is used to locate the Radio Button option containing the value Bird.



CssSelector is used to find the required Web Page element (option) and click on that option. "tag[attribute='value']" is used as the CssSelector in the absence of ID or ClassName. 
The above code snippet will select the Bird option by using the click() method provided by Selenium WebDriver.


That's it in this post. Hope you are following the sequence of posts in this blog for better understanding of Test Automation using Selenium.
Don't forget to subscribe to our YouTube Channel for better understanding via videos and more examples.

Happy Learning.

YouTube Channel : Click Here

Friday, 2 June 2017

Selenium WebDriver : Using Navigation methods

Want to navigate to a particular Website (or URL), and you are feeling a bit lazy to open a browser, type in the website URL again and again.. and again.. whenever you want to ? Welcome to another post in which we are going to see how we can use selenium WebDriver to open a Website. 

If you are new to Selenium, check out my previous posts on how to set up the environment to get started with Selenium WebDriver.


Selenium WebDriver provides us with different methods such as get() and navigate(), which can be used directly to get the website (or URL) specified as parameters with these methods.


1 - Using get() method :

     Following code snippet can be used to get a webpage after initializing Web Browser :

 

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/");
}
}


Note : get() takes the URL of the website as a parameter.

 


2- Using navigate() method :

      Following code snippet can be used as an alternative method to get() :

 

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.navigate().to("https://www.google.com/");

driver.findElement(By.cssSelector("#_eEe a")).click();
driver.navigate().back();
driver.navigate().forward();
}
}


Note : navigate().to() takes the URL of the website as a parameter.

 

If you use navigate().to() method to get a web page, it also allows you to use couple of more methods to navigate through the history of the Web Browser. So you would be able to go back or forward, as far as Web Browser history is concerned.

 

Using back() : (To navigate back in history)

driver.navigate().back();

 

Using forward() : (To navigate forward in history)

driver.navigate().forward();

 

Above 2 mentioned methods (back()  and forward()) allows you to navigate through the history of the Web Browser as and when required.

 

That's it in this short post about navigating to Web Pages using Selenium WebDriver. Feel free to comment in the comments section if you have any queries / suggestions.

Stay tuned for more interesting posts about Test Automation using Selenium.

Happy Learning.  

YouTube Channel : Click Here

Friday, 26 May 2017

Selenium WebDriver : Setting / Getting Browser Window Dimensions

What's up Gangsters ? Welcome to another post in which we will see how we can set the dimensions of our browser window as required. Either we can set the dimensions to some absolute values or we can maximize the window.



Setting Browser window dimensions :


Following code snippet shows how to set the dimensions using absolute values after initializing Web Browser :



System.setProperty("webdriver.gecko.driver", "E:\\path\\to\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
org.openqa.selenium.Dimension d = new org.openqa.selenium.Dimension(400, 500);
driver.manage().window().setSize(d);



Note :  org.openqa.selenium is the package name that includes Dimension as one of the classes to be used to set dimensions in Selenium and driver is the WebDriver instance.


Another way to set the dimensions of Browser window is to maximize the size of the window. Following code snippet shows how to maximize the browser window:



driver.manage().window().maximize();




The above methods can be used to set the dimensions of the Browser window based on the requirement.




Getting Browser window dimensions :


Let's now see how we can get the dimensions of the window at any given point. Believe me it's really simple (Just one line of code :P). Following code snippet can be used to get the dimensions of the Browser window and print it on console :



System.out.println(driver.manage().window().getSize());



Told you it's going to be just one line of code, not so complex right ? :)


To conclude, in this post we have seen how we can set the dimensions of the Browser window after we initialize the driver instance, either by using the absolute values or maximizing to the size of the desktop. Also we have seen how we can get the dimensions of the window at any given point.


Feel free to comment if you have any doubts / suggestions.
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

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

Thursday, 21 May 2015

Selenium - Test Automation Tool Introduction

Selenium is a Software Test Automation Framework used to automate Web-based Applications. It is a collection of tools which can be used to perform various tasks.

Selenium is used to automate test cases related to Web Pages that are frozen and will not change in the near future. We can say that "Manual Testing" can be partially replaced by "Selenium (Automation)" to test most of the features on Web Pages.


Why To Automate ?


The first question that comes in our mind is: Why there is a need to automate Web-based Applications? There are lots of things on a web page which requires our attention as far as testing is concerned, in order to deliver a perfect error-free optimized product. In such cases, it is difficult to track each and everything on a Website (presence of text boxes, functionality of buttons, etc).
For such reasons, Selenium was developed so that anyone with/without programming background can test basic functionalities/UI (User Interface) elements and include such automated test cases in Sanity/Regression suites!! Even if you are not familiar with programming, don't worry, Selenium provides its hassle free "Record And Playback" feature via Selenium-IDE.
Another reason is, Automation Scripts are not error prone when required to be executed multiple times, which allows us to use one of the Test Automation tool.



What To Automate ?

  • Functionalities that are not going to change in the near future.
  • Regression Testing.
  • Test Cases that takes more time to execute.
  • Test Cases that are error-prone and are very important.
  • Test Cases that require huge amount of input data.
  • Test Cases that includes Repeated Tasks.


What NOT To Automate ?

  • Functionalities that change frequently.
  • When a quick manual testing will do the job instead of Automation Script.
  • Testing the functionality for the first time.


Selenium Testing Framework Tools

Selenium IDE :

  • Very important tool for those who are not familiar with Programming.
  • Provides "Record and Playback" feature.
  • Contains various other features such as running a single/multiple test cases, test suites, controlling speed of execution, finding and highlighting UI elements and many more.
  • Its a Firefox Add-On.

Selenium RC :

  • It requires a standalone server for its execution
  • Needs a programming background.
Officially it has been deprecated so we will not go in detail.

Selenium WebDriver :

  • One of the most popular tool for Test Automation.
  • Programming background is needed.
  • Supports programming languages such as Java, Python, C#, etc.
  • Can automate almost any task on any Web Application.
  • Unlike Selenium RC, it doesn't require standalone server for its execution.

Selenium Grid :

  • Used to perform automation on remote machines.
  • Consist of one Hub and multiple Nodes.
  • Hub controls the execution of code on various Nodes.
  • This helps in parallel automation on different browsers and machines.


Will discuss these in upcoming posts.
Happy Learning.



YouTube Channel : Click Here