http://blog.csdn.net/berniebd/article/details/6395012
1.Click on link
-
WebElement oClickElem = oWebDriver.findElement(By.linkText("Next"));
-
oClickElem.click();
2.Type characters
-
oInputElement = oWebDriver.findElement(By.xpath("//*[@name='first-name']")); // XPATH locator.
-
oInputElement.sendKeys("Xuan");
-
-
oInputElement = oWebDriver.findElement(By.cssSelector("input[name='last-name']"));
-
oInputElement.sendKeys("Ngo");
-
-
oInputElement = oWebDriver.findElement(By.className("red"));
-
oInputElement.sendKeys("my password");
-
-
oInputElement = oWebDriver.findElement(By.id("unique-id"));
-
oInputElement.sendKeys("中文");
-
-
oInputElement = oWebDriver.findElement(By.name("key-press"));
-
oInputElement.sendKeys("auto complete");
3.Check radio buttons and checkboxes
-
-
WebElement oRadioBtn = oWebDriver.findElement(By.xpath("//input[@value='Mon']"));
-
oRadioBtn.click();
-
-
-
WebElement oCheckBoxApple = oWebDriver.findElement(By.cssSelector("input[name='apple']"));
-
oCheckBoxApple.click();
-
-
-
WebElement oCheckBoxOrange = oWebDriver.findElement(By.cssSelector("input[name='orange']"));
-
oCheckBoxOrange.click();
4.Select single and multiple options
-
-
-
-
-
Select oSingleSelection = new Select(oWebDriver.findElement(By.id("single-selection")));
-
-
-
oSingleSelection.selectByVisibleText("July");
-
-
-
-
-
-
Select oMulitpleSelection = new Select(oWebDriver.findElement(By.xpath("//select[@multiple='multiple' and @size=12]")));
-
-
-
oMulitpleSelection.deselectAll();
-
-
-
oMulitpleSelection.selectByIndex(1);
-
oMulitpleSelection.selectByValue("Aug");
-
oMulitpleSelection.selectByVisibleText("November");
-
5.Overloading findElement() to add waiting time
-
-
-
-
-
-
-
-
private WebElement findElement(WebDriver webDriver, By by, int timeout)
-
{
-
int iSleepTime = 1000;
-
for(int i=0; i
-
{
-
List oWebElements = webDriver.findElements(by);
-
if(oWebElements.size()>0)
-
{
-
return oWebElements.get(0);
-
}
-
else
-
{
-
try
-
{
-
Thread.sleep(iSleepTime);
-
System.out.println(String.format("%s: Waited for %d milliseconds.[%s]", new java.util.Date().toString(), i+iSleepTime, by));
-
}
-
catch(InterruptedException ex)
-
{
-
throw new RuntimeException(ex);
-
}
-
}
-
}
-
-
-
String sException = String.format("Can't find %s after %d milliseconds.", by, timeout);
-
throw new RuntimeException(sException);
-
}
6.Click on any html element using different locators
-
WebElement oClickElem = null;
-
-
-
oClickElem = oWebDriver.findElement(By.name("submit_button"));
-
oClickElem.click();
-
pause(1000);
-
-
-
oClickElem = oWebDriver.findElement(By.id("unique-link-id"));
-
oClickElem.click();
-
pause(1000);
-
-
-
oClickElem = oWebDriver.findElement(By.xpath("//li[contains(text(), 'using the xpath')]")); // Find Next link.
-
oClickElem.click();
-
pause(3000);
7.自定义暂停
-
public static void pause(final int iTimeInMillis)
-
{
-
try
-
{
-
Thread.sleep(iTimeInMillis);
-
}
-
catch(InterruptedException ex)
-
{
-
System.out.println(ex.getMessage());
-
}
-
}
1.button
-
//button[contains(text(),'someText')]
阅读(2510) | 评论(1) | 转发(0) |