import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import com.v9.h3c.imc.common.V9GlobalSettings;
public class Browsers {
public WebDriver driver = null;
private FirefoxProfile firefoxprofile = null;
private static DesiredCapabilities caps =null;
private String projectpath = System.getProperty("user.dir");
public Browsers(BrowsersType browserstype){
switch(browserstype){
case firefox:
System.setProperty("webdriver.firefox.bin", V9GlobalSettings.firefoxPath);
File firebug = new File(projectpath+"\\tool\\firebug.xpi");
File firepath = new File(projectpath+"\\tool\\FireXPath.xpi");
firefoxprofile = new FirefoxProfile();
try {
firefoxprofile.addExtension(firebug);
firefoxprofile.addExtension(firepath);
} catch (IOException e) {
e.printStackTrace();
}
driver = new FirefoxDriver(firefoxprofile);
driver.manage().window().maximize();
break;
case ie:
System.getProperty("webdriver.ie.driver", projectpath+V9GlobalSettings.ieDriverPath);
caps = DesiredCapabilities.internetExplorer();
caps.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
caps.setCapability(InternetExplorerDriver.IE_SWITCHES, "-private");
caps.setCapability("ignoreZoomSetting", true);
driver = new InternetExplorerDriver(caps);
driver.manage().window().maximize();
break;
case chrome:
System.setProperty("webdriver.chrome.driver", projectpath+V9GlobalSettings.chromeDriverPath);
caps = DesiredCapabilities.chrome();
caps.setCapability("chrome.switches", Arrays.asList("--start-maximized"));
driver = new ChromeDriver(caps);
driver.manage().window().maximize();break;
}
}
}
一个selenium的浏览器封装
最新推荐文章于 2024-08-14 14:56:00 发布
本文介绍了一个用于浏览器自动化测试的Java类实现。该类支持Firefox、Internet Explorer及Chrome三种浏览器,并通过设置不同的配置项来启动浏览器实例。针对每种浏览器,文章详细展示了如何加载特定配置文件、扩展插件以及最大化窗口。
摘要由CSDN通过智能技术生成