1.打開此網頁 http://nanjing.xiaomishu.com/shop/search/sp2048_745
2.向下拖動滾動條,右下角自動會出現【投訴與建議】(此網頁已經修改不拖動也出現了,以前是沒有的,)
3.點選【投訴與建議】
4.在打開的div 層中輸入姓名,郵件,内容 并點選确定
5.驗證頁面上的提示文字
package com.example.tests;
import org.junit.*;
import org.openqa.selenium.*;
import org.openqa.selenium.ie.internetexplorerdriver;
public class seleniumwebdriver {
public static webdriver driver;
@test
public void testunit() {
driver = new internetexplorerdriver();
driver.get(http://nanjing.xiaomishu.com/shop/search/sp2048_745);
maxbrowser(driver);
setscroll(driver,500);
driver.findelement(by.linktext("投訴與建議")).click();
driver.findelement(by.xpath("//input[@id='repname']")).sendkeys("1");
driver.findelement(by.xpath("//input[@id='repmail']")).sendkeys("1");
driver.findelement(by.xpath("//textarea[@id='repcontent']")).sendkeys("hello");
driver.findelement(by.xpath("//a[@id='repbtn']")).click();
assert.assertequals("您輸入的郵箱格式不正确", driver.findelement(by.xpath("//div[@id='floatbox_remind']/span")).gettext());
}
//将ie最大化
public static void maxbrowser(webdriver driver){
try {
string maxbroswer = "if (window.screen) {window.moveto(0, 0);" +
"window.resizeto(window.screen.availwidth,window.screen.availheight);}";
javascriptexecutor jse=(javascriptexecutor) driver;
jse.executescript(maxbroswer);
} catch (exception e) {
system.out.println("fail to maximization browser");
}
}
//将滾動條滾到适合的位置
public static void setscroll(webdriver driver,int height){
try {
string setscroll = "document.documentelement.scrolltop=" + height;
javascriptexecutor jse=(javascriptexecutor) driver;
jse.executescript(setscroll);
} catch (exception e) {
system.out.println("fail to set the scroll.");
}
}
}
對于這種頂級div層,一開始用id來定位,在firefox中可以正常跑
在ie中會報stack overflow的錯誤,一直以來是以為沒有加等待時間而沒找到
後來試了很多方法,最後發現用xpath就解決了,真是耽誤了很久的時間
給大家借鑒,如果一個定位方法不能用時,多換換其他的
這個例子中我們學習了如何用js控制滾動條,如何最大化ie頁面。
最新内容請見作者的github頁:http://qaseven.github.io/