3.3 Selenide啟動多瀏覽器測試
Selenide上面已經講過,我們添加Selenide在搭建好的maven工程中
Add these lines to file pom.xml:
<dependency>
<groupId>com.codeborne</groupId>
<artifactId>selenide</artifactId>
<version>4.5.1</version>
<scope>test</scope>
</dependency>
等待jar包全部下載完成后,點擊maven Dependencies查看已經增加了Seleide環境所需的所有依賴jar包
Selenide安裝完成后,接下來嘗試啟動瀏覽器,目前最常見的瀏覽器為Chrom/IE/firefox, Selenide默認啟動為Firefox瀏覽器,不需要依賴瀏覽器驅動,並且每次啟動的瀏覽器都是沒有任何插件和cookies信息的瀏覽器。因為Selenide不需要實例化對象,所有都需要靜態導入import static com.codeborne.selenide.Selenide.* 來調用提供常用的api
啟動Firefox瀏覽器
package StartDriver;
import static com.codeborne.selenide.Selenide.* ;
import org.testng.annotations.Test;
public class TestStartDriver {
@Test
public void testFirefox(){
open("https://www.baidu.com/");
}
}
啟動Chrome/IE等其他的瀏覽器時需要驅動支持,下載驅動可以自行百度下載,選擇對應的版本以及操作系統環境,已chrome為例:
https://sites.google.com/a/chromium.org/chromedriver/
注意Chrome Driver與Chrome版本需要對應起來,按照所需下載對應的driver驅動
啟動Chrome瀏覽器
package StartDriver;
import static com.codeborne.selenide.Selenide.* ;
import org.testng.annotations.Test;
public class TestStartDriver {
@Test
public void testFirefox(){
open("https://www.baidu.com/");
}
@Test void testChrome(){
//指定Driver 的存放目錄
System.setProperty("webdriver.chrome.driver","c:/driver/chromedriver.exe");
//指定需要啟動的瀏覽器類型
System.setProperty("selenide.browser", "Chrome");
open("https://www.baidu.com/");
}
}
啟動IE瀏覽器
這里注意的是IE驅動區分32/64位操作系統
http://selenium-release.storage.googleapis.com/index.html
@Test void testIE(){
//指定Driver 的存放目錄
System.setProperty("webdriver.chrome.driver","c:/driver/IEDriverServer.exe");
//指定需要啟動的瀏覽器類型
System.setProperty("selenide.browser", "Ie");
open("https://www.baidu.com/");
}
帶插件的啟動Chrome
在真實測試場景中,我們可能對測試有不同需求,我看你需要在測試中需要對測試結果做一些處理,比如抓包,解析,截圖等等,而chrome提供了很多好好用的插件,所以帶插件啟動也是一種比較常用測試手段
@Test
public void testChromeExt(){
//指定Driver 的存放目錄
System.setProperty("webdriver.chrome.driver","c:/driver/chromedriver.exe");
//指定創建的安裝位置
ChromeOptions options = new ChromeOptions();
options.addExtensions(new File("c:/driver/jsonview.crx"));
WebDriver webDriver = new ChromeDriver(options);
setWebDriver(webDriver);
open("https://www.baidu.com/");
}
帶配置的啟動Chrome
@Test
public void testChromeProfile(){
//指定Driver 的存放目錄
System.setProperty("webdriver.chrome.driver","c:/driver/chromedriver.exe");
//指定創建的安裝位置
ChromeOptions options = new ChromeOptions();
options.addArguments("--test-type");
WebDriver webDriver = new ChromeDriver(options);
setWebDriver(webDriver);
open("https://www.baidu.com/");
}
當然啟動瀏覽器的方式在我遇到的測試場景還有更多中,情況。比如代理,瀏覽器模式,瀏覽器大小等,后面我會在再補充。
任務目標:
下載並不同瀏覽器插件並自己完成上述所有不同的啟動方式