問(wèn)題一:
如何獲取新打開(kāi)的瀏覽器窗口,用selenium進(jìn)行UI自動(dòng)化測(cè)試時(shí),經(jīng)常會(huì)遇到這個(gè)問(wèn)題,現(xiàn)總結(jié)如下:
// 存儲(chǔ)現(xiàn)在窗口句柄
String winHandleBefore = driver.getWindowHandle();
//跳轉(zhuǎn)到新打開(kāi)的窗口
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)操作
問(wèn)題二:
在項(xiàng)目中添加log時(shí)出現(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.
費(fèi)勁周折也沒(méi)找到原因:后在Stack Overflow上找到了答案,希望與君共勉。
有兩種方法:
第一種:在賣(mài)你代碼中加上如下代碼
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
問(wèn)題三:
eclipse項(xiàng)目報(bào)錯(cuò)如下所示:
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
原因是項(xiàng)目所在目錄中.classpath文件中存在如下聲明:
。ㄖ皼](méi)有在maven中添加selenium的依賴,手動(dòng)添加的selenium的環(huán)境變量,當(dāng)用maven添加selenium依賴后,將此目錄中的selenium刪除,eclipse找不到手動(dòng)設(shè)置的seleniumjar包的位置導(dǎo)致出錯(cuò)。),將此行刪掉問(wèn)題解決了。
<classpathentry kind="lib" path="D:/Selenium_workspace/MyProgram/src/test/resource/selenium-server-standalone-2.45.0.jar"/>