問題一:
如何獲取新打開的瀏覽器窗口,用selenium進(jìn)行UI自動化測試時,經(jīng)常會遇到這個問題,現(xiàn)總結(jié)如下:
// 存儲現(xiàn)在窗口句柄
String winHandleBefore = driver.getWindowHandle();
//跳轉(zhuǎn)到新打開的窗口
for(String winHandle : driver.getWindowHandles()){
driver.switchTo().window(winHandle);
}
// 執(zhí)行相關(guān)操作
...
// 關(guān)掉當(dāng)前新的窗口
driver.close();
// 跳轉(zhuǎn)到之前的窗口
driver.switchTo().window(winHandleBefore);
// 接著進(jìn)行相關(guān)操作
問題二:
在項目中添加log時出現(xiàn)了WARN,如下所示:
log4j:WARN No appenders could be found for logger .
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
費勁周折也沒找到原因:后在Stack Overflow上找到了答案,希望與君共勉。
有兩種方法:
第一種:在賣你代碼中加上如下代碼
BasicConfigurator.configure();
第二種:添加如下標(biāo)準(zhǔn)log4j.properties文件到classpath
# Set root logger level to DEBUG and its only appender to A1.
log4j.rootLogger=DEBUG, A1
# A1 is set to be a ConsoleAppender.
log4j.appender.A1=org.apache.log4j.ConsoleAppender
# A1 uses PatternLayout.
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
問題三:
eclipse項目報錯如下所示:
Project 'MyProgram' is missing required library: 'D:Selenium_workspaceMyProgramsrc est
esourceselenium-server-standalone-2.45.0.jar'
The project cannot be built until build path errors are resolved
原因是項目所在目錄中.classpath文件中存在如下聲明:
。ㄖ皼]有在maven中添加selenium的依賴,手動添加的selenium的環(huán)境變量,當(dāng)用maven添加selenium依賴后,將此目錄中的selenium刪除,eclipse找不到手動設(shè)置的seleniumjar包的位置導(dǎo)致出錯。),將此行刪掉問題解決了。
<classpathentry kind="lib" path="D:/Selenium_workspace/MyProgram/src/test/resource/selenium-server-standalone-2.45.0.jar"/>