Pages

Monday 9 December 2013

Sunday 8 December 2013

How I can select a value in dropdownlist that is not visible in selenium?

As display is set to 'none' for 'Select' tag, Selenium is unable to identify dropdown list values.
One way of handling is by clicking that dropdown and enabling the list.
Other way is by executing JavaScript to enable the dropdown list and then selecting value using WebDriver's 'Select' API.

Here is the code to handle using JavaScript:
 JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("document.getElementById('country').style.display='block';");

//Then Select required value
Select dropdown = new Select(driver.findElement(By.id("country")));
dropdown.selectByVisibleText("India");  


Assuming HTML code for select is as below:

Saturday 19 May 2012

How to check the presence of Alert (isAlertPresent) using WebDriver?

Below method will return true if the alert got displayed:

public boolean isAlertPresent() {

  boolean presentFlag = false;

  try {

   // Check the presence of alert
   Alert alert = driver.switchTo().alert();
   // Alert present; set the flag
   presentFlag = true;
   // if present consume the alert
   alert.accept();

  } catch (NoAlertPresentException ex) {
   // Alert not present
   ex.printStackTrace();
  }

  return presentFlag;

 }
Note - Thank you Ganesh S for asking this query.

Friday 9 March 2012

How can I execute Javascript?

We can execute Javascript by casting the WebDriver instance to a JavascriptExecutor:

//Instantiate any WebDriver; Eg. Firefox
 WebDriver driver = new FirefoxDriver();
 driver.get("http://seleniumwebdriverfaq.tumblr.com/");
 //Casting the WebDriver instance to a JavascriptExecutor
 JavascriptExecutor js = (JavascriptExecutor) driver;
 //Execute the JavaScript - Eg. to get the page title
 js.executeScript("return document.title");
 //Print the out put
 System.out.println("Page title from Java script: "
      +js.executeScript("return document.title"));
 System.out.println("Page title from driver.getTitle() : "
      +driver.getTitle());
 //JUnit Assertion
 Assert.assertEquals(js.executeScript
    ("return document.title"),driver.getTitle());

How to take screenshot of the test page?

We can take entire page screenshot by following code:


   //Instantiate any WebDriver; Eg. Firefox
WebDriver driver = new FirefoxDriver();
driver.get("http://seleniumwebdriverfaq.tumblr.com/");
//Get the screenshot as file
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
// Copy the screenshot to your file system
FileUtils.copyFile(scrFile, new File("c:\screenshots\screenshot.png"));

How can I start Firefox with add-ons installed?

Create a Firefox profile and add required add-ons. 


Take ‘Firebug’ as an example, save the Firebug XPI into the C:FF_addons folder as firebug.xpi (Download XPI file from Firebug download page, right-click on the “Download Now” and save as C:FF_addonsfirebug.xpi).


Then create a Firefox profile and add Firebug add-on while instantiating FirefoxDriver as below:


   String firebugFilePath = "C:\FF_addons\firebug.xpi";
FirefoxProfile profile = new FirefoxProfile();
profile.addExtension(new File(firebugFilePath));
// Add more FF addons if required
WebDriver driver = new FirefoxDriver(profile);

Thursday 8 March 2012

How can I handle alert dialog box?

Below method will handle the alert dialog displayed and also it will return the alert message displayed.

public String getAlert() { 
     Alert alert = driver.switchTo().alert(); 
     String alertMsg = alert.getText();
     alert.accept();
     return alertMsg; }