@Test
public void testUIComputation() throws Exception {
// populate text fields with values
populate();
// trigger computation by using the button
//点击控件计算两个数的和
WebElement button = driver.findElement(By.className("UIAButton"));
button.click();
// is sum equal ?
//判断得到的数是否和正确的和相等
WebElement texts = driver.findElement(By.className("UIAStaticText"));
assertEquals(String.valueOf(values.get(0) + values.get(1)), texts.getText());
}
@Test
public void testBasicAlert() throws Exception {
//点击alert控件,弹出提示框
driver.findElement(By.xpath("//UIAButton[2]")).click();
Alert alert = driver.switchTo().alert();
//check if title of alert is correct
//验证提示框文字是否正确
assertEquals("Cool title this alert is so cool.", alert.getText());
alert.accept();
}
@Test
public void testBasicButton() throws Exception {
// 验证求和 button文字显示是否正确
WebElement button = driver.findElement(By.xpath("//UIAButton[1]"));
assertEquals("Compute Sum", button.getText());
}
@Test
public void testClear() throws Exception {
//验证编辑框输入清空后编辑框内容是否为空
WebElement text = driver.findElement(By.xpath("//UIATextField[1]"));
text.sendKeys("12");
text.clear();
assertEquals("", text.getText());
}
@Test
public void testHideKeyboard() throws Exception {
//编辑框输入文字
driver.findElement(By.xpath("//UIATextField[1]")).sendKeys("12");
//确认键盘是否弹出
WebElement button = driver.findElement(MobileBy.AccessibilityId("Done"));
assertTrue(button.isDisplayed());
//点击隐藏键盘
button.click();
}
@Test
public void testFindElementByClassName() throws Exception {
Random random = new Random();
//通过classname定位控件测试
WebElement text = driver.findElementByClassName("UIATextField");
int number = random.nextInt(MAXIMUM - MINIMUM + 1) + MINIMUM;
text.sendKeys(String.valueOf(number));
driver.findElementByClassName("UIAButton").click();
// is sum equal ?
//验证是否相等
WebElement sumLabel = driver.findElementByClassName("UIAStaticText");
assertEquals(String.valueOf(number), sumLabel.getText());
}
@Test
public void testFindElementsByClassName() throws Exception {
Random random = new Random();
//通过classname定位控件测试
WebElement text = (WebElement) driver.findElementsByClassName("UIATextField").get(1);
int number = random.nextInt(MAXIMUM - MINIMUM + 1) + MINIMUM;
text.sendKeys(String.valueOf(number));
driver.findElementByClassName("UIAButton").click();
// is sum equal ?
WebElement sumLabel = (WebElement) driver.findElementsByClassName("UIAStaticText").get(0);
assertEquals(String.valueOf(number), sumLabel.getText());
}
@Test
public void testAttribute() throws Exception {
Random random = new Random();
WebElement text = driver.findElement(By.xpath("//UIATextField[1]"));
//编辑框输入文字
int number = random.nextInt(MAXIMUM - MINIMUM + 1) + MINIMUM;
text.sendKeys(String.valueOf(number));
//验证控件的name,label,value等属性是否和设定的相同,name为IntegerA
assertEquals("IntegerA", text.getAttribute("name"));
assertEquals("TextField1", text.getAttribute("label"));
assertEquals(String.valueOf(number), text.getAttribute("value"));
}
@Test
public void testSlider() throws Exception {
//get the slider
//进度条控件
WebElement slider = driver.findElement(By.xpath("//UIASlider[1]"));
// 查看初始数据是否为50%
assertEquals("50%", slider.getAttribute("value"));
Point sliderLocation = getCenter(slider);
//拖动控件,从中心拖到初始位置
driver.swipe(sliderLocation.getX(), sliderLocation.getY(), sliderLocation.getX()-100, sliderLocation.getY(), 1000);
//验证控件是否归0,不归0?
assertEquals("0%", slider.getAttribute("value"));
}
@Test
public void testLocation() throws Exception {
//验证控件的位置是否为94,122?
WebElement button = driver.findElement(By.xpath("//UIAButton[1]"));
Point location = button.getLocation();
assertEquals(94, location.getX());
assertEquals(142, location.getY());
}
@Test
public void testSessions() throws Exception {
// ?
HttpGet request = new HttpGet("http://localhost:4723/wd/hub/sessions");
@SuppressWarnings("resource")
HttpClient httpClient = new DefaultHttpClient();
HttpResponse response = httpClient.execute(request);
HttpEntity entity = response.getEntity();
JSONObject jsonObject = (JSONObject) new JSONParser().parse(EntityUtils.toString(entity));
String sessionId = driver.getSessionId().toString();
assertEquals(jsonObject.get("sessionId"), sessionId);
}
@Test
public void testSize() {
//验证两个编辑框控件大小是否一致
Dimension text1 = driver.findElement(By.xpath("//UIATextField[1]")).getSize();
Dimension text2 = driver.findElement(By.xpath("//UIATextField[2]")).getSize();
assertEquals(text1.getWidth(), text2.getWidth());
assertEquals(text1.getHeight(), text2.getHeight());
}