List of usage examples for org.openqa.selenium WebElement clear
void clear();
From source file:com.sol.androidphone.ActionBot.java
License:Apache License
/** * Type text into the specified element// w w w . j a v a 2s. c om * @param element - element to be typed into * @param textToType - text to be typed */ public void type(final WebElement element, final String textToType) { element.clear(); element.sendKeys(textToType); }
From source file:com.sol.androidphone.ActionBot.java
License:Apache License
/** * Type text into the specified element//from ww w. j a va2 s.co m * @param element - element to be typed into * @param textToType - text to be typed * @param toClearBefore - if true the element will be cleared before typing * (as in the method clear()). false indicates that the method will not be * cleared */ public void type(final WebElement element, final String textToType, boolean toClearBefore) { if (toClearBefore) { element.clear(); } element.sendKeys(textToType); }
From source file:com.sol.browser.ActionBot.java
License:Apache License
/** * Type something into an input field. WebDriver doesn't normally clear these * before typing, so this method does that first. * @param locator - locator of the element to be typed into (e.g. search * bar)/*from w ww .j ava 2 s .c o m*/ * @param text - text to be written into the element */ public void type(final By locator, final String text) { WebElement element = driver.findElement(locator); element.clear(); element.sendKeys(text); }
From source file:com.sonar.it.jenkins.orchestrator.JenkinsOrchestrator.java
License:Open Source License
/** * Used to fix false positive about strange chars added by sendKey * http://code.google.com/p/selenium/issues/detail?id=4446 * Update: it is no more necessary as I have disabled native events but I keep it just in case */// w w w. j av a2s. c om public void setTextValue(final WebElement element, final String text) { (new WebDriverWait(driver, 10)).until(new Function<WebDriver, Boolean>() { @Override public Boolean apply(WebDriver input) { element.clear(); element.sendKeys(text); return element.getAttribute("value").equals(text); } }); }
From source file:com.sugarcrm.candybean.examples.mobile.AppiumAndroidTest.java
License:Open Source License
@Test public void testClear() throws Exception { WebElement text = driver.findElement(By.xpath("//textfield[1]")); text.sendKeys("12"); text.clear(); assertEquals(text.getText(), ""); }
From source file:com.sugarcrm.candybean.examples.mobile.SugarAndroidTest.java
License:Open Source License
@Test public void testLogin() throws Exception { WebElement username = driver.findElement(By.xpath("//window[1]/scrollview[1]/webview[1]/textfield[1]")); assertTrue(username.isDisplayed());/*w ww . j av a 2 s .com*/ Set<String> handles = driver.getWindowHandles(); for (String s : handles) { System.out.println(s); } if (!username.getText().equals("")) { username.clear(); } username.sendKeys("admin"); WebElement password = driver.findElement(By.xpath("//window[1]/scrollview[1]/webview[1]/secure[1]")); assertTrue(password.isDisplayed()); password.click(); password.sendKeys("asdf"); WebElement login = driver.findElement(By.xpath("//window[1]/scrollview[1]/webview[1]/link[1]")); login.click(); Thread.sleep(1000000); }
From source file:com.sugarcrm.candybean.examples.mobile.SugarIosTest.java
License:Open Source License
@Test public void testLogin() throws CandybeanException { WebElement username = iface.wd.findElement(By.xpath("//window[1]/scrollview[1]/webview[1]/textfield[1]")); assertTrue(username.isDisplayed());/*w w w . jav a 2 s.com*/ Set<String> handles = iface.wd.getWindowHandles(); for (String s : handles) { System.out.println(s); } if (!username.getText().equals("")) { username.clear(); } username.sendKeys("admin"); WebElement password = iface.wd.findElement(By.xpath("//window[1]/scrollview[1]/webview[1]/secure[1]")); assertTrue(password.isDisplayed()); password.click(); password.sendKeys("asdf"); WebElement login = iface.wd.findElement(By.xpath("//window[1]/scrollview[1]/webview[1]/link[1]")); login.click(); iface.pause(1000000); }
From source file:com.sugarcrm.candybean.examples.sugar.SugarAndroidTest.java
License:Open Source License
@Test public void testLogin() throws Exception { WebElement username = driver.findElement(By.xpath("//window[1]/scrollview[1]/webview[1]/textfield[1]")); assertTrue(username.isDisplayed());//from ww w. j a v a 2 s . co m Set<String> handles = driver.getWindowHandles(); for (String s : handles) { System.out.println(s); } if (!username.getText().equals("")) { username.clear(); } username.sendKeys("admin"); WebElement password = driver.findElement(By.xpath("//window[1]/scrollview[1]/webview[1]/secure[1]")); assertTrue(password.isDisplayed()); password.click(); password.sendKeys("asdf"); WebElement login = driver.findElement(By.xpath("//window[1]/scrollview[1]/webview[1]/link[1]")); login.click(); Thread.sleep(1000000); }
From source file:com.testmax.runner.SeleniumTestRunner.java
License:CDDL license
protected WebElement executeWhile() throws TestMaxException { long executionTime = System.currentTimeMillis(); List<WebElement> controls = null; try {// www.ja v a 2s. com this.setImpilicitTimeInMiliSec(new Integer(timeout)); controls = this.getWebElements(); for (WebElement control : controls) { this.setImpilicitTimeInMiliSec(new Integer(timeout)); if (method.equalsIgnoreCase("click")) { try { printMessage("**** Clicking " + this.controlIdentifier); control.click(); printMessage("**** Clicked " + this.controlIdentifier + ":" + addMessage()); //do not return to continue with while } catch (Exception e) { return control; //WebElement target=executeClick(control); //do not return to continue with while } } else if (method.equalsIgnoreCase("selectByValue")) { this.setImpilicitTimeInMiliSec(new Integer(timeout)); while (!control.isDisplayed()) { this.setImpilicitTimeInMiliSec(new Integer(timeout)); } new Select(control).selectByValue(sendkeyValue); this.timeTaken = this.timeTaken + (System.currentTimeMillis() - executionTime); printMessage("**** Selected Value= " + addMessage() + " for Control=" + this.controlIdentifier + ":" + addMessage()); return control; } else if (method.equalsIgnoreCase("deselectByVisibleText")) { this.setImpilicitTimeInMiliSec(new Integer(timeout)); while (!control.isDisplayed()) { this.setImpilicitTimeInMiliSec(new Integer(timeout)); } new Select(control).deselectByVisibleText(sendkeyValue); this.timeTaken = this.timeTaken + (System.currentTimeMillis() - executionTime); printMessage("**** Deselected Value= " + addMessage() + " for Control=" + this.controlIdentifier + ":" + addMessage()); return control; } else if (method.equalsIgnoreCase("selectByVisibleText")) { this.setImpilicitTimeInMiliSec(new Integer(timeout)); while (!control.isDisplayed()) { this.setImpilicitTimeInMiliSec(new Integer(timeout)); } new Select(control).selectByVisibleText(sendkeyValue); this.timeTaken = this.timeTaken + (System.currentTimeMillis() - executionTime); printMessage("**** Selected Value= " + addMessage() + " for Control=" + this.controlIdentifier + ":" + addMessage()); return control; } else if (method.equalsIgnoreCase("clear")) { this.setImpilicitTimeInMiliSec(new Integer(timeout)); while (!control.isDisplayed()) { this.setImpilicitTimeInMiliSec(new Integer(timeout)); } Thread.sleep(100); control.clear(); this.timeTaken = this.timeTaken + (System.currentTimeMillis() - executionTime); printMessage("**** Cleared " + this.controlIdentifier + ":" + addMessage()); return control; } else if (method.equalsIgnoreCase("movemouse")) { this.setImpilicitTimeInMiliSec(new Integer(timeout)); while (!control.isDisplayed()) { this.setImpilicitTimeInMiliSec(new Integer(timeout)); } Thread.sleep(100); this.mouseMove(control); this.timeTaken = this.timeTaken + (System.currentTimeMillis() - executionTime); printMessage("**** Moved Mouse " + this.controlIdentifier + ":" + addMessage()); return control; } else if (method.equalsIgnoreCase("submit")) { this.setImpilicitTimeInMiliSec(new Integer(timeout)); while (!control.isDisplayed()) { this.setImpilicitTimeInMiliSec(new Integer(timeout)); } Thread.sleep(100); control.submit(); this.timeTaken = this.timeTaken + (System.currentTimeMillis() - executionTime); printMessage("**** Submitted " + this.controlIdentifier + ":" + addMessage()); return control; } else if (method.equalsIgnoreCase("asserttext")) { try { this.setImpilicitTimeInMiliSec(new Integer(timeout)); while (!control.isDisplayed()) { this.setImpilicitTimeInMiliSec(new Integer(timeout)); } Thread.sleep(100); String actual = control.getText(); this.timeTaken = this.timeTaken + (System.currentTimeMillis() - executionTime); printMessage("**** verifying Text Value " + actual + " for " + this.controlIdentifier + ":" + addMessage()); if (actual != null && !actual.isEmpty() && actual.contains(this.stripchar)) { if (value != null && value.contains("@")) { this.addTestExtract(value, actual); } else { this.assertText(value, actual); } return control; } } catch (Exception e) { } } else if (method.equalsIgnoreCase("sendKeys")) { this.setImpilicitTimeInMiliSec(new Integer(timeout)); while (!control.isDisplayed()) { this.setImpilicitTimeInMiliSec(new Integer(timeout)); } Thread.sleep(100); control.sendKeys(sendkeyValue); this.timeTaken = this.timeTaken + (System.currentTimeMillis() - executionTime); printMessage("**** Entered Value= " + addMessage() + " for Control=" + this.controlIdentifier + ":" + addMessage()); return control; } } } catch (NumberFormatException e) { // TODO Auto-generated catch block this.printCommand( " FAILED with Exception for Command=" + this.tagdesc + " for control with identifier=" + this.controlIdentifier + " file=" + addMessage() + "\n Message=" + e.getMessage()); this.printCommand(WmLog.getStackTrace(e)); String v_msg = ">>Dataset:" + this.threadIndex + " FAILED TO EXECUTE CONTROL IN " + addMessage() + " " + (e.getMessage() != null && e.getMessage().length() > 100 ? e.getMessage().substring(0, 100) : e.getMessage()); e.printStackTrace(); throw new TestMaxException(v_msg); //handleTimeOut(v_msg); } catch (InterruptedException e) { // TODO Auto-generated catch block this.printCommand( " FAILED with Exception for Command=" + this.tagdesc + " for control with identifier=" + this.controlIdentifier + " file=" + addMessage() + "\n Message=" + e.getMessage()); this.printCommand(WmLog.getStackTrace(e)); String v_msg = ">>Dataset:" + this.threadIndex + " FAILED TO EXECUTE CONTROL IN " + addMessage() + " " + (e.getMessage() != null && e.getMessage().length() > 100 ? e.getMessage().substring(0, 100) : e.getMessage()); e.printStackTrace(); throw new TestMaxException(v_msg); //handleTimeOut(v_msg); } catch (Exception e) { // TODO Auto-generated catch block this.printCommand( " FAILED with Exception for Command=" + this.tagdesc + " for control with identifier=" + this.controlIdentifier + " file=" + addMessage() + "\n Message=" + e.getMessage()); this.printCommand(WmLog.getStackTrace(e)); String v_msg = ">>Dataset:" + this.threadIndex + " FAILED TO EXECUTE CONTROL IN " + addMessage() + " " + (e.getMessage() != null && e.getMessage().length() > 100 ? e.getMessage().substring(0, 100) : e.getMessage()); e.printStackTrace(); throw new TestMaxException(v_msg); //handleTimeOut(v_msg); } //if you are here then quit return null; }
From source file:com.testmax.runner.SeleniumTestRunner.java
License:CDDL license
protected WebElement executeMobileAll() throws TestMaxException { long executionTime = System.currentTimeMillis(); List<WebElement> controls = null; try {//from ww w .j av a2s . co m this.setImpilicitTimeInMiliSec(new Integer(timeout)); //handle custom operations if (this.isBypassCustomMethod()) { if (method.equalsIgnoreCase("get")) { this.setImpilicitTimeInMiliSec(new Integer(timeout)); this.waitToPageLoad(new Integer(timeout)); if (!url.contains("http") && !url.contains("file:")) { url = this.baseUrl + url; } url = url.replace("[env]", ConfigLoader.getConfig("QA_TEST_ENV")); if (this.baseUrl.contains("https")) { url = url.replace("http:", "https:"); } this.printCommand(this.tagdesc + " URL=" + url); driver.get(url); this.timeTaken = this.timeTaken + (System.currentTimeMillis() - executionTime); printMessage("**** Executed Open URL= " + url + " for Control=" + this.controlIdentifier + ":" + addMessage()); return null; } else if (method.equalsIgnoreCase("closewindow")) { this.driver.close(); return null; } else if (method.equalsIgnoreCase("sleep")) { this.executeSleep(); return null; } else if (method.equalsIgnoreCase("executescript")) { this.setImpilicitTimeInMiliSec(new Integer(timeout)); Thread.sleep(100); this.executeScript(); this.timeTaken = this.timeTaken + (System.currentTimeMillis() - executionTime); this.printCommand(this.tagdesc + " for control with identifier=" + this.controlIdentifier + "\n Script=" + addMessage()); //printMessage("**** Executed Java Script= "+addMessage()+ " for Control="+this.controlIdentifier +":"+ addMessage()); return null; } else if (method.equalsIgnoreCase("executeoperation")) { this.setImpilicitTimeInMiliSec(new Integer(timeout)); Thread.sleep(100); this.executeOperation(); this.timeTaken = this.timeTaken + (System.currentTimeMillis() - executionTime); this.printCommand(this.tagdesc + " for control with identifier=" + this.controlIdentifier + " value=" + addMessage()); //printMessage("**** Executed Java Script= "+addMessage()+ " for Control="+this.controlIdentifier +":"+ addMessage()); return null; } else if (this.method.equalsIgnoreCase("urlextract")) { String currenturl = this.driver.getCurrentUrl(); if (currenturl != null && !currenturl.isEmpty()) { this.addTestExtract(this.value, currenturl); printMessage("#### Extracted parameters from URL=" + currenturl); } return null; } else if (method.equalsIgnoreCase("closewindow")) { this.driver.close(); return null; } } controls = this.getWebElements(); for (WebElement control : controls) { this.setImpilicitTimeInMiliSec(new Integer(timeout)); if (!isBypassDisplay()) { while (!control.isDisplayed()) { this.setImpilicitTimeInMiliSec(new Integer(timeout)); } } if (method.equalsIgnoreCase("click")) { try { printMessage("**** Clicking " + this.controlIdentifier); this.printCommand(this.tagdesc + " for control with identifier=" + this.controlIdentifier); control.click(); printMessage("**** Clicked " + this.controlIdentifier + ":" + addMessage()); return control; } catch (Exception e) { WebElement target = executeClick(control); if (target != null) { return target; } } } else if (method.equalsIgnoreCase("selectByValue")) { this.printCommand(this.tagdesc + " for control with identifier=" + this.controlIdentifier + " value=" + addMessage()); this.setImpilicitTimeInMiliSec(new Integer(timeout)); while (!control.isDisplayed()) { this.setImpilicitTimeInMiliSec(new Integer(timeout)); } new Select(control).selectByValue(sendkeyValue); this.timeTaken = this.timeTaken + (System.currentTimeMillis() - executionTime); printMessage("**** Selected Value= " + addMessage() + " for Control=" + this.controlIdentifier + ":" + addMessage()); return control; } else if (method.equalsIgnoreCase("deselectByVisibleText")) { this.setImpilicitTimeInMiliSec(new Integer(timeout)); while (!control.isDisplayed()) { this.setImpilicitTimeInMiliSec(new Integer(timeout)); } new Select(control).deselectByVisibleText(sendkeyValue); this.timeTaken = this.timeTaken + (System.currentTimeMillis() - executionTime); printMessage("**** Deselected Value= " + addMessage() + " for Control=" + this.controlIdentifier + ":" + addMessage()); return control; } else if (method.equalsIgnoreCase("selectByVisibleText")) { this.printCommand(this.tagdesc + " for control with identifier=" + this.controlIdentifier + " value=" + addMessage()); this.setImpilicitTimeInMiliSec(new Integer(timeout)); while (!control.isDisplayed()) { this.setImpilicitTimeInMiliSec(new Integer(timeout)); } new Select(control).selectByVisibleText(sendkeyValue); this.timeTaken = this.timeTaken + (System.currentTimeMillis() - executionTime); printMessage("**** Selected Value= " + addMessage() + " for Control=" + this.controlIdentifier + ":" + addMessage()); return control; } else if (method.equalsIgnoreCase("clear")) { try { this.printCommand(this.tagdesc + " for control with identifier=" + this.controlIdentifier); this.setImpilicitTimeInMiliSec(new Integer(timeout)); while (!control.isDisplayed()) { this.setImpilicitTimeInMiliSec(new Integer(timeout)); } Thread.sleep(100); if (control.isEnabled()) control.clear(); this.timeTaken = this.timeTaken + (System.currentTimeMillis() - executionTime); printMessage("**** Cleared " + this.controlIdentifier + ":" + addMessage()); return control; } catch (Exception e) { this.clearElement(); Thread.sleep(500); return control; } } else if (method.equalsIgnoreCase("movemouse")) { this.printCommand(this.tagdesc + " for control with identifier=" + this.controlIdentifier); this.setImpilicitTimeInMiliSec(new Integer(timeout)); while (!control.isDisplayed()) { this.setImpilicitTimeInMiliSec(new Integer(timeout)); } Thread.sleep(100); this.mouseMove(control); this.timeTaken = this.timeTaken + (System.currentTimeMillis() - executionTime); printMessage("**** Moved Mouse " + this.controlIdentifier + ":" + addMessage()); return control; } else if (method.equalsIgnoreCase("download")) { this.printCommand(this.tagdesc + " for control with identifier=" + this.controlIdentifier); this.setImpilicitTimeInMiliSec(new Integer(timeout)); while (!control.isDisplayed()) { this.setImpilicitTimeInMiliSec(new Integer(timeout)); } Thread.sleep(100); this.downloadFile(control); if (this.value != null && !this.value.isEmpty() && this.value.contains("[")) { this.modifyexcel(this.value); } this.timeTaken = this.timeTaken + (System.currentTimeMillis() - executionTime); printMessage("**** Download File " + this.controlIdentifier + ":" + addMessage()); return control; } else if (method.equalsIgnoreCase("uploadfile")) { this.setImpilicitTimeInMiliSec(new Integer(timeout)); while (!control.isDisplayed()) { this.setImpilicitTimeInMiliSec(new Integer(timeout)); } Thread.sleep(100); String dest = ConfigLoader.getWmOutputWebServicePath() + "modifytest.xls"; File mfile = new File(dest); String modifyfile = mfile.getCanonicalPath(); this.printCommand(this.tagdesc + " for control with identifier=" + this.controlIdentifier + " file=" + modifyfile); control.sendKeys(modifyfile); this.timeTaken = this.timeTaken + (System.currentTimeMillis() - executionTime); printMessage("**** Entered Value= " + addMessage() + " for Control=" + this.controlIdentifier + ":" + addMessage()); return control; } else if (method.equalsIgnoreCase("submit")) { this.printCommand(this.tagdesc + " for control with identifier=" + this.controlIdentifier); this.setImpilicitTimeInMiliSec(new Integer(timeout)); while (!control.isDisplayed()) { this.setImpilicitTimeInMiliSec(new Integer(timeout)); } Thread.sleep(100); control.submit(); this.timeTaken = this.timeTaken + (System.currentTimeMillis() - executionTime); printMessage("**** Submitted " + this.controlIdentifier + ":" + addMessage()); return control; } else if (method.equalsIgnoreCase("asserttext")) { try { this.setImpilicitTimeInMiliSec(new Integer(timeout)); Thread.sleep(100); String actual = control.getText(); if (actual == null || actual.isEmpty()) { actual = control.getAttribute("value"); printMessage( "**** Control won't have Text Value. Extracted Value using attribute Name=value. Found value= " + actual + " for " + this.controlIdentifier + ":" + addMessage()); } this.timeTaken = this.timeTaken + (System.currentTimeMillis() - executionTime); this.printCommand(this.tagdesc + " for control with identifier=" + this.controlIdentifier + " value=" + addMessage() + ", actual=" + actual); printMessage("**** verifying Text Value= " + actual + " for " + this.controlIdentifier + ":" + addMessage()); if (actual != null && !actual.isEmpty()) { if (value != null && value.contains("@")) { this.addTestExtract(value, actual); } else { this.assertText(value, actual); } return control; } } catch (Exception e) { } } else if (method.equalsIgnoreCase("sendKeys")) { this.printCommand(this.tagdesc + " for control with identifier=" + this.controlIdentifier + " value=" + addMessage()); this.setImpilicitTimeInMiliSec(new Integer(timeout)); while (!control.isDisplayed()) { this.setImpilicitTimeInMiliSec(new Integer(timeout)); } Thread.sleep(100); control.sendKeys(sendkeyValue); this.timeTaken = this.timeTaken + (System.currentTimeMillis() - executionTime); printMessage("**** Entered Value= " + addMessage() + " for Control=" + this.controlIdentifier + ":" + addMessage()); return control; } } } catch (NumberFormatException e) { this.printCommand( " FAILED with Exception for Command=" + this.tagdesc + " for control with identifier=" + this.controlIdentifier + " file=" + addMessage() + "\n Message=" + e.getMessage()); this.printCommand(WmLog.getStackTrace(e)); // TODO Auto-generated catch block String v_msg = ">>Dataset:" + this.threadIndex + " FAILED TO EXECUTE CONTROL IN " + addMessage() + " " + (e.getMessage() != null && e.getMessage().length() > 100 ? e.getMessage().substring(0, 100) : e.getMessage()); e.printStackTrace(); throw new TestMaxException(v_msg); //handleTimeOut(v_msg); } catch (InterruptedException e) { // TODO Auto-generated catch block this.printCommand( " FAILED with Exception for Command=" + this.tagdesc + " for control with identifier=" + this.controlIdentifier + " file=" + addMessage() + "\n Message=" + e.getMessage()); this.printCommand(WmLog.getStackTrace(e)); String v_msg = ">>Dataset:" + this.threadIndex + " FAILED TO EXECUTE CONTROL IN " + addMessage() + " " + (e.getMessage() != null && e.getMessage().length() > 100 ? e.getMessage().substring(0, 100) : e.getMessage()); e.printStackTrace(); throw new TestMaxException(v_msg); //handleTimeOut(v_msg); } catch (Exception e) { // TODO Auto-generated catch block this.printCommand( " FAILED with Exception for Command=" + this.tagdesc + " for control with identifier=" + this.controlIdentifier + " file=" + addMessage() + "\n Message=" + e.getMessage()); this.printCommand(WmLog.getStackTrace(e)); String v_msg = ">>Dataset:" + this.threadIndex + " FAILED TO EXECUTE CONTROL IN " + addMessage() + " " + (e.getMessage() != null && e.getMessage().length() > 100 ? e.getMessage().substring(0, 100) : e.getMessage()); e.printStackTrace(); throw new TestMaxException(v_msg); //handleTimeOut(v_msg); } //if you are here then quit //driver.quit(); return null; }