Showing posts with label web page. Show all posts
Showing posts with label web page. Show all posts

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, 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