默認啟動firefox瀏覽器
Webdriver driver = new FirefoxDriver();
啟動谷歌瀏覽器
配置chromedriver
WebDriver driver; System.setProperty("webdriver.chrome.driver", chromedriver_path); driver = new ChromeDriver();
修改User-Agent來偽裝瀏覽器訪問手機站點
有時候為了測試需要,可能需要使用測試手機wap這樣的站點,如果用真正的手機去測試也可以實現,但是比較麻煩,我們可以通過設置chrome的user agent來偽裝瀏覽器,達到我們的測試目的。
具體實現代碼:
public static void main(String[] args) { //設置webdriver.chrome.driver屬性 System.setProperty("webdriver,chrome.driver", "ddriver/chromedriver.exe"); //聲明chromeoptions,主要是給chrome設置參數 ChromeOptions options = new ChromeOptions(); //設置user agent為iphone5 options.addArguments("--user-agent=Apple Iphone 5"); //實例化chrome對象,並加入選項 WebDriver driver = new ChromeDriver(options); //打開百度 driver.get("https://www.baidu.com"); try{ Thread.sleep(5000); }catch(InterruptedException e) { e.printStackTrace(); } ddriver.quit(); }
啟動IE瀏覽器
配置iedriver
WebDriver driver; System.setProperty("webdriver.ie.driver", iedriver); //IE的常規設置,便於執行自動化測試 DesiredCapabilities ieCapabilities = DesiredCapabilities.internetExplorer(); ieCapabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true); driver= new InternetExplorerDriver(ieCapabilities);