是否可以在Firefox上运行我的Selenium测试,然后在Chrome和IE上执行相同的测试?我怎样才能做到这一点?Selenium Firefox然后Chrome和IE
回答
使用Java,你可以自动处理所需的二进制文件(chromedriver,geckodriver和IEDriverServer.exe)通过。
看一个完整的例子作为参数化的JUnit测试用例。注意,测试代码是单一的,而在测试参数(方法data()
),你选择要运行的代码(浏览器,Firefox和Internet Explorer)浏览器:
@RunWith(Parameterized.class)
public class MultipleBrowsersTest {
protected WebDriver driver;
@Parameter
public Class<? extends WebDriver> driverClass;
@Parameters(name = "{index}: {0}")
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][] { { ChromeDriver.class }, { FirefoxDriver.class },
{ InternetExplorerDriver.class } });
}
@Before
public void setupTest() throws Exception {
WebDriverManager.getInstance(driverClass).setup();
driver = driverClass.newInstance();
}
@After
public void teardown() {
if (driver != null) {
driver.quit();
}
}
@Test
public void test() {
// Your test code here
}
}
WebDriver是一个界面,其中FirefoxDriver,ChromeDriver,InternetExplorerDriver在上实现。这些浏览器特定的驱动程序实现WebDriver界面中定义的所有方法。作为用户,您只使用WebDriver的方法(通过使用WebDriver对象调用它们),并且已被每个驱动程序如何实现这些方法所隐藏。所以,你不必担心每个方法的实现。
但是你必须指定的唯一的事情如下:
对Firefox浏览器运行测试:
WebDriver driver = new ChromeDriver();
运行:
WebDriver driver = new FirefoxDriver();
对Firefox浏览器运行测试互联网浏览器测试:
WebDriver driver = new InternetExplorerDriver();
同样,对于几乎所有的浏览器,我们都有相应的Driver类可用。
除了WebDriver语言库之外,还需要为每个浏览器下载.exe文件。
geckodriver.exe - firefox 48.0 and above (before 48 version, firefox has built-in support and no need of geckodriver.exe
chromedriver.exe - all chrome versions
InternetExplorerDriver.exe - all IE versions
,一旦你下载他们,你让硒知道在哪里,这些可执行文件被保存在:
System.setProperty("browser_key", "/path/to/the/driver")
example:
System.setProperty("webdriver.firefox.marionette","G:\\Selenium\\Firefox driver\\geckodriver.exe"); // to set path to firefox driver, similary for chrome and IE browser.
remember key changes based on the browser.
或者,你可以保持在PATH环境变量中的.exe(在Windows中)或其他操作系统中的类似功能。
一旦获得驱动程序实例,就可以使用WebDriver接口上可用的所有方法。(these are browser independent)
例如:
driver.get("http://www.google.com") // to lauch browser and visit google page
WebElement searchField = driver.findElement(By.id("SearchInput")) // to find an element in a page. returns a WebElement object.
searchField.sendKeys("search something") // to enter text into input field.
参考文献:
我使用的是和的NodeJS我通过foreach做到了这一点。
const browsers = ['ie','firefox','chrome'];
browsers.forEach(function(item, index){
Tests(item,i);
});
function Tests(item, index) {
describe("This is a test", function() {});
}
然后我把项目在浏览器的名称应该是在你的代码
...然后在相同的测试上铬和IE
铬 - 有一个开源Selenium IDE for Chrome在Chrome商店中可用作Kantu Browser Automation plus Selenium IDE
源代码在Github下gpl许可证。
- 1. IE,Firefox,Chrome,Opera和Safari
- 2. Firefox和Chrome/IE版式
- 3. Python Selenium - 适用于Chrome,Firefox和IE的通用脚本
- 4. Selenium Webdriver打开Firefox,然后死亡
- 5. jQuery animate()在Firefox和Chrome/IE中不同
- 6. JS:在IE错误,在Chrome和Firefox
- 7. IE条件注释和Chrome/Firefox的
- 8. 在IE,Chrome和Firefox中自动执行右键点击保存?
- 9. Chrome和Selenium
- 10. jQuery的问题与IE浏览器...仍然工作在Firefox和Chrome的
- 11. 在IE和Firefox
- 12. Firefox和Chrome Selenium webdrivers与水豚和SitePrism的行为不同
- 13. @在Firefox和Chrome
- 14. 与Chrome和IE
- 15. Link在Chrome和Firefox没有点击,但在Opera和IE
- 16. 如何在Chrome和IE/Edge中显示.webp和Firefox中的.jpg?
- 17. JavaScript工作在IE和Edge上,但不适用于Chrome和Firefox
- 18. 对Chrome,IE,Firefox的CSS检查
- 19. @ font-face不适用于Chrome,Firefox,IE
- 20. 在Selenium RC中加载FireFox和IE的页面
- 21. Selenium CSS定位器,包含Firefox和IE上的onclick
- 22. Iframe和Firefox/IE bug
- 23. Html.BeginForm后要HTTPGET行动,而不是HttpPost在IE中,罚款Chrome和Firefox
- 24. Selenium sendKeys()针对Chrome,Firefox和Safari的不同行为
- 25. 使用Capybara,Selenium,Ruby启动IE和Chrome浏览器
- 26. jQuery的,Firefox和Chrome
- 27. HSTS在Firefox和Chrome
- 28. 形象Firefox和Chrome
- 29. lastchild.appendchild在Firefox和Chrome
- 30. Decimal.Parse在Chrome和Firefox
指定硒版本,语言及其版本(如java,python)和浏览器版本的具体答案。 –