No.of links on a page and display the links text:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class GetTagName {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");
List<WebElement> searchButton = driver.findElements(By.tagName("a"));
System.out.println(searchButton.size());
for(int i=0;i<searchButton.size();i++)
System.out.println(searchButton.get(i).getText()); }
}
Is Displayed, Is enabled, is selected,
driver.get("http://www.google.com");
WebElement searchButton = driver.findElement(By.name("btnK"));
System.out.println(searchButton.isDisplayed());
}
Send keys:
driver.get("http://www.google.com");
WebElement searchBox = driver.findElement(By.name("q"));
searchBox.sendKeys(Keys.chord(Keys.SHIFT, "packt publishing"));
searchBox.submit();
}
XPath- single slash and double slash
/
- start selection from the document node
- allows you to create 'absolute' path expressions
- e.g. “/html/body/p” matches all the paragraph elements
//
- start selection matching anywhere in the document
- allows you to create 'relative' path expressions
- e.g. “//p” matches all the paragraph elements
Handling Alerts:
Selenium can handle java script alerts in a very effective manner but it can not handle modal alert box which gets created by OS itself e.g.: java script error message alert. Now how do you handle these java script alerts, what can you do with those alerts, lets see one by one with the code.
Scenario as an example -:
Select a object from the page, Click on Delete button. It triggers a alert saying "Object is going to be deleted from the database, Click OK to Confirm or Cancel to Cancel the Operation".
We need to test the message of the Alert, Clicking Ok and Clicking Cancel also.
Steps to be Performed
Select the Object to delete from the page.
Click on Delete button, it triggers the Alert message.
Select a object from the page, Click on Delete button. It triggers a alert saying "Object is going to be deleted from the database, Click OK to Confirm or Cancel to Cancel the Operation".
We need to test the message of the Alert, Clicking Ok and Clicking Cancel also.
Steps to be Performed
Select the Object to delete from the page.
Click on Delete button, it triggers the Alert message.
Sample code:
/* Code to switch the control to Alert message*/
Alert testAlrt=driver.switchTo().alert();
testAlrt is the Controlling object of the Alert. Now you can use this object to Accept/get Text/Dismiss the alert.
/* To get the text from the alert */
String sAltText=testAlrt.getText();
/* To Accept any alert, it means Clicking OK button */
testAlrt.accept();
/* To dismiss any alert, It means Clicking Cancel button */
testAlrt.dismiss();
/* If the Alerts gets created using Ajax call, then you may need to wait till the Alert present */
WebDriverWait wdWait= new WebDriverWait(driver, 10);
wdWait.until(ExpectedConditions.alertIsPresent());
/* Code to switch the control to Alert message*/
Alert testAlrt=driver.switchTo().alert();
testAlrt is the Controlling object of the Alert. Now you can use this object to Accept/get Text/Dismiss the alert.
/* To get the text from the alert */
String sAltText=testAlrt.getText();
/* To Accept any alert, it means Clicking OK button */
testAlrt.accept();
/* To dismiss any alert, It means Clicking Cancel button */
testAlrt.dismiss();
/* If the Alerts gets created using Ajax call, then you may need to wait till the Alert present */
WebDriverWait wdWait= new WebDriverWait(driver, 10);
wdWait.until(ExpectedConditions.alertIsPresent());
Expected Wait class:
Implementing wait for element to appear using WebDriver wait and ExpectedCondition class ie Implicit and Explicit wait.
That is only to find the element in a web page or wait till it appear in the DOM. But using ExpectedCondition class, you can wait for an alert to be present, for an element to be staled while loading another page, for an element to be ready for a click operation, and many more.
Let us see some of the code to implement above things. As an example, lets have a situation where you perform some action on a element and it reloads the frame/page.
How do you know that the page is reloaded after performing the action?
Simple answer is if the page is reloaded then the existing element will be staled right? Ok so lets see how we can verify that.
sample code-:
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement TestElement = driver.findElement(By any locator) ;
// perform some action on the element to reload the page
wait.until(ExpectedConditions. stalesnessOf(TestElement ));
Untrusted SSL certificates using selenium webdriver in IE?
Some time we get these SSL errors or notification when we try to execute our scripts,At that time we need to by pass that one
SSL error in IE In the following way..
To By pass it just need to add a small piece of code when ever it occurs.
i.e
try {
driver.get("javascript:document.getElementById('overridelink').click();");
} catch (Exception e) {
}
Handling windows and frames
It's rare for a modern web application not to have any frames or to be constrained to a single window. WebDriver supports moving between named windows using the "switchTo" method:
driver.switchTo ().window ("windowName");
All calls to driver will now be interpreted as being directed to the particular window. But how do you know the window's name? Take a look at the javascript or link that opened it:
<a href="somewhere.html" target="windowName">Click here to open a new window</a>
Alternatively, you can pass a "window handle" to the "switchTo().window()" method. Knowing this, it's possible to iterate over every open window like so:
for (String handle : driver.getWindowHandles()) {
driver.switchTo().window(handle);
}
driver.switchTo().window(handle);
}
You can also swing from frame to frame (or into iframes):
driver.switchTo().frame("frameName");
It's possible to access subframes by chaining switchTo() calls, and you can specify the frame by its index too. That is:
driver.switchTo().frame("frameName")
.switchTo().frame(0)
.switchTo().frame("child");
.switchTo().frame(0)
.switchTo().frame("child");
would go to the frame named "child" of the first subframe of the frame called "frameName". All frames are evaluated as if from currently switched to frame. To get back to the top level, call:
driver.switchTo().defaultContent();