Showing posts with label get. Show all posts
Showing posts with label get. Show all posts

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

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