Friday, 17 October 2014

Few useful Commands in Selenium Webdriver

Open Browser
WebDriver driver = new FirefoxDriver();
For other browser and details are discussed in my earlier blog. Please have a look here.

Open Page URL
Get” method is used to open a particular url in browser.
driver.get("https://www.gmail.com");

Passing values in text field
sendKeys” keyword is used to enter values to the field.
driver.findElement(By.id("Email")).sendKeys("testuser@gmail.com");


Clear value from Text field
clear() method is used to clear the field.
driver.findElement(By.id("Email")).clear();

Clicking button
Here click() method is used to click a button.
driver.findElement(By.id("signIn")).click();


Submit button
In the above case, Sign in is a button. But works as a submit button too. So, below code will work as well.
driver.findElement(By.id("signIn")).submit();

Selecting value from drop-down
We have 3 different methods to select the drop-down value.
selectByIndex() → Here index is the order in drop-down, and 1st element is always having index 0.
selectByVisibleText() → It is the visible text , what we see in drop-down list and want to select.
selectByValue() → It is the option value, which can be viewed in Firebug inspector

Look at these option values, we got through Firebug inspector for that particular dropdown.

<option value="">Select Country</option>
<option value="111">Denmark</option>
<option value="120">Sweden</option>
<option value="127">Normay</option>
<option value="139">Germany</option>
<option value="143">Paris</option>
<option value="219">Austria</option>
<option value="224">Spain</option>


Let see an example here of selecting value by 3 ways.

Select selectBox = new Select(driver.findElement(By.id("Country")); // Here dropdown field is clicked
selectBox.selectByIndex(0); // Select through Index
“OR”
selectBox.selectByVisibleText(“Denmark”); //Select through visible Text
“OR”
selectBox.selectByValue(111); //Select through option value

Deselecting value from drop-down
Here also we have 3 different methods to select the drop-down value.
deselectByIndex() → Here index is the order in drop-down, and 1st element is always having index 0.
deselectByVisibleText() → It is the visible text , what we see in drop-down list and want to select.
deselectByValue() → It is the option value, which can be viewed in Firebug inspector

Here command is also almost same as selecting drop-down.
selectBox.deselectByIndex(0); // Deselect through Index
“OR”
selectBox.deselectByVisibleText(“Denmark”); //Deselect through visible Text
“OR”
selectBox.deselectByValue(111); //Deselect through option value

Selecting/Deselecting Check box
Here click() method is used to toggle on/off for checkbox.
driver.findElement(By.id("PersistentCookie")).click();


Selecting Radio button
Here also, it will work in same way of checkbox.
driver.findElement(By.id("Ad_Type_1")).click();

Accepting a Confirm box
driver.findElement(By.id(("Delete_Ad"))).click(); // Here the Alert window is opened on clicking   button
Alert alert = driver.switchTo().alert(); 
alert.accept(); // This line is to accept the alert box, means clicking Ok.


For Alert and Prompt boxes also, It will work in same way.

Rejecting a Confirm box
Here, also it will work in same way as of Accepting confirm box.
driver.findElement(By.id(("Delete_Ad"))).click(); // Here the Alert window is opened on clicking   button
Alert alert = driver.switchTo().alert();
alert.dismiss();  // This line is to click 'Cancel' in the confirm box.

For Prompt boxes also, it will work in same way.
And in Alert box, we don't have cancel button to reject.

Maximize screen
Here is the command to maximize the brower window.
driver.manage().window().maximize();

Navigate
Here are few commands related to navigation.
driver.navigate().to("http://thetestzing.blogspot.in//2014/10/locating-web-elements-in-selenium.html"); // Here it will navigate to the particular page.
driver.navigate().back(); // Here it is same as clicking browser back button
driver.navigate().forward(); // Here it is same as clicking browser forward button
driver.navigate().refresh(); // Here it is same as refreshing/reloading the browser page.

Verify Element
Sometimes in our application, we may need some verification. Like after logging into a site, we can verify whether the login name present or not and we can confirm that correct user is logged in.

Lets see the example below.

WebElement Element = driver.findElement(By.xpath("//*[@id='Text1']/div[1]/span"));

Boolean iselementpresent = Element.isEnabled();
if (iselementpresent == true) {
System.out.println("Text is--"+ Element.getText());
}
else{
System.out.println("Element not found");
}
Here in the 1st line we found the xpath for a text element and stored that path in a web element.
Then created one boolean object which will accept true or false value.
Then in If-Else block we compared that boolean value. If it is true, then it will print that text, else will print the message mentioned.

1 comment :