Thursday, June 26, 2014

Page Refresh in Selenium WebDriver

There are many ways that we can refresh a web page in Selenium Webdriver tests. Here are listed some of them: 
1) Using refresh() method of WebDriver
driver.navigate().refresh();
This is most commonly used method. 
2) Using F5 key
driver.findElement(By.name("q")).sendKeys(Keys.F5);
This is also commonly used method. We must use it in any text field of the web page as it uses Send keys method. 
3) Using Actions class of WebDriver
Actions actions = new Actions(driver);
actions.keyDown(Keys.CONTROL).sendKeys(Keys.F5).perform();
4) Using jQuery
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("history.go(0)");
5) Using navigate( ).to( ) method of WebDriver
driver.navigate().to(driver.getCurrentUrl());
6) Using ASCII code
driver.findElement(By.name("q")).sendKeys("\uE035");

Navigate Back, Forward and to an Url in Selenium Webdriver

Below scenarios explains how we can navigate across the browser for various web pages.
WebDriver driver = new FirefoxDriver();
driver.get("http://aksahu.blogspot.in/");

Navigate to an Url:
driver.navigate().to("http://knowledgebase-wiki.appspot.com/");
This navigates the page to "http://knowledgebase-wiki.appspot.com/"

Navigate Back:
driver.navigate().back();
This navigates back to the previous page "http://aksahu.blogspot.in/"

Navigate Forward:
driver.navigate().forward();
This navigates the page forward to "http://knowledgebase-wiki.appspot.com/"

Friday, May 16, 2014

Array to List and List to Array in Java

While testing web applications we came across many list of elements or an element with list of data. In such cases we may need the following conversation instead of iterating over an array or a list.
 
Please refer the below simple example to convert a string array to a list and vice versa. But be careful about the order of elements, If required you may use collection methods to do so.

Lets take an arry of strings as below
String[] elementsAsArray = new String[]{"ele1","ele2","ele3"};

Array to List:
List arrayAsList = Arrays.asList(elementsAsArray);

List to Array:
String[] listAsArray = arrayAsList.toArray(new String[arrayAsList.size()]);

Sunday, March 16, 2014

Switch between windows or tabs in Selenium Webdriver

Please refer the below set of code to understand how can we switch between different windows in Selenium. Open a web page:
WebDriver driver = new FirefoxDriver();
driver.get("http://aksahu.blogspot.in/");
Open a new window:
WebElement elemLink = driver.findElement(By.linkText("Web Driver"));
Actions actions = new Actions(driver);
actions.moveToElement(elemLink);
actions.contextClick(elemLink).sendKeys(Keys.ARROW_DOWN)
  .sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ENTER).build()
  .perform();
For opening in a new tab/window see the post Open in new Tab or new Window in Selenium Webdriver

Switch between two tabs or windows:

(1) 
If you want to switch to a specific window, see below:
String windowHandle = driver.getWindowHandle();

Now if you want to switch to second windows use the below code:
for(String winHandle : driver.getWindowHandles()){
    driver.switchTo().window(winHandle);
}
Switch to the parent window:
driver.switchTo().window(windowHandle);

(2)
Or, simply you can do as below to switch between two tabs or windows:
ArrayList tabs = new ArrayList (driver.getWindowHandles());

//Switch to new window
driver.switchTo().window(tabs.get(1));
driver.close();//do some action in new window(2nd tab)

//Switch to main/parent window
driver.switchTo().window(tabs.get(0));
driver.getTitle();//do some action in main window(1st tab)

Saturday, March 15, 2014

Open in new Tab or new Window in Selenium Webdriver

New Tab:
Use the below code to complete the action in a new tab.
WebDriver driver = new FirefoxDriver();
driver.get("http://aksahu.blogspot.in/");
You can  do by either of the below ways
driver.findElement(By.linkText("Tips & Tricks")).sendKeys(Keys.CONTROL + "t");
Or
WebElement aLink = driver.findElement(By.linkText("Tips & Tricks"));
Actions actions = new Actions(driver);
actions.moveToElement(aLink);
actions.contextClick(aLink)
       .sendKeys(Keys.ARROW_DOWN)
       .sendKeys(Keys.ENTER).build().perform();

New Window:
Use the below code to complete the action in a new window.
WebDriver driver = new FirefoxDriver();
driver.get("http://aksahu.blogspot.in/");
Open a web page in a new window:
String script = "var d=document,a=d.createElement('a');a.target='_blank';a.href='%s';a.innerHTML='.';d.body.appendChild(a);return a";
Object element = ((JavascriptExecutor) driver).executeScript(String.format(script, "http://aksahu.blogspot.in/"));
if (element instanceof WebElement) {
    WebElement anchor = (WebElement) element;
    anchor.click();
    ((JavascriptExecutor) driver).executeScript("var a=arguments[0];a.parentNode.removeChild(a);", anchor);
} else {
     throw new JavaScriptException(element, "Unable to open Window", 1);
}
Open a link in a new window:
WebElement elemLink = driver.findElement(By.linkText("Web Driver"));
Actions actions = new Actions(driver);
actions.moveToElement(elemLink);
actions.contextClick(elemLink)
       .sendKeys(Keys.ARROW_DOWN)
       .sendKeys(Keys.ARROW_DOWN)
       .sendKeys(Keys.ENTER).build().perform();