Wednesday, December 11, 2013

Confirm text is displayed in webpage

public boolean isTextPresent(String text) {
    List<WebElement> foundElements = driver.findElements(By.xpath 
("//*[contains(text(), '" + text + "')]"));
    return foundElements.size() > 0;
}

Tuesday, December 3, 2013

Handling Web Popup dialog boxes and Windows Authentication popup

Alert alert = driver.switchTo().alert();
      alert.accept();
(OR)
            alert.dismiss();
(OR)
            alert.read();
(OR)
     alert.getText();
(OR)
driver.switchTo().activeElement();
             
(OR)
 
public boolean isAlertPresent() {
 
           boolean presentFlag = false;
           String alterTitle = null;
 
           try {
 
                // Check the presence of alert
                Alert alert = driver.switchTo().alert();
                // Alert present; set the flag
                return true;
               
           } catch (NoAlertPresentException e) {
                return false;    
    }
          
       }
(OR)
public boolean isAlertPresent(int timeoutSec) {
            WebDriverWait wait = new WebDriverWait(driver, timeoutSec);
             try {
                 wait.until(ExpectedConditions.alertIsPresent());
                 return true;
             } catch (TimeoutException e) {
                 return false;
             }
       }

(OR)
public static void  handleAlert(WebDriver driver, boolean acceptAlert) {
        try {
            Alert alert = driver.switchTo().alert();
            if (acceptAlert) {
                alert.accept();
            } else {
                alert.dismiss();
            }
           
        } catch (NoAlertPresentException e) {
            
        }
    }


Handle Windows Authentication popup using Selenium Webdriver:

public static void handler() throws IOException
{
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
Runtime.getRuntime().exec("D:\\Scripts\\AutoITScript\\HandleAuthentication.exe");
  driver.get("https://www.engprod-charter.net/");
 }
 
AutoIT Script:
WinWaitActive("Authentication Required","","10")
If WinExists("Authentication Required") Then
Send("username{TAB}")
Send("Password{Enter}")
EndIf’
 
 
 By passing UsenName and Password in the URl:
public static final String TEST_ENVIRONMENT = "the-site.com";

public void login(String uname, String pwd){
String URL = "http://" + uname + ":" + pwd + "@" + TEST_ENVIRONMENT;
driver.get(URL);
Thread.sleep(5000);
}

Robot script:
public void login() throws Exception {

//wait – increase this wait period if required
Thread.sleep(5000);

//create robot for keyboard operations
Robot rb = new Robot();

//Enter user name by ctrl-v
StringSelection username = new StringSelection(“username”);
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(username, null);
rb.keyPress(KeyEvent.VK_CONTROL);
rb.keyPress(KeyEvent.VK_V);
rb.keyRelease(KeyEvent.VK_V);
rb.keyRelease(KeyEvent.VK_CONTROL);

//tab to password entry field
rb.keyPress(KeyEvent.VK_TAB);
rb.keyRelease(KeyEvent.VK_TAB);
Thread.sleep(2000);

//Enter password by ctrl-v
StringSelection pwd = new StringSelection(“password”);
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(pwd, null);
rb.keyPress(KeyEvent.VK_CONTROL);
rb.keyPress(KeyEvent.VK_V);
rb.keyRelease(KeyEvent.VK_V);
rb.keyRelease(KeyEvent.VK_CONTROL);

//press enter
rb.keyPress(KeyEvent.VK_ENTER);
rb.keyRelease(KeyEvent.VK_ENTER);

//wait
Thread.sleep(5000);
}