是否可以在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. JS:在IE錯誤,在Chrome和Firefox
- 6. 在Chrome和Firefox但不是IE
- 7. IE條件註釋和Chrome/Firefox的
- 8. jQuery animate()在Firefox和Chrome/IE中不同
- 9. 網站顯示在IE比Chrome和Firefox
- 10. 如何捕獲後退按鈕事件在Chrome,Firefox和IE
- 11. 多平臺(Chrome,IE,Firefox)
- 12. 可以點擊IE和Firefox中的複選框,但不是在Chrome中 - Selenium(C#)
- 13. 在IE,Chrome和Firefox中自動執行右鍵點擊保存?
- 14. Chrome和Selenium
- 15. jQuery的問題與IE瀏覽器...仍然工作在Firefox和Chrome的
- 16. 在IE和Firefox
- 17. IE瀏覽器在使用selenium testng指定firefox後打開
- 18. Firefox和Chrome Selenium webdrivers與水豚和SitePrism的行爲不同
- 19. 與Chrome和IE
- 20. 在Chrome和Firefox
- 21. @在Firefox和Chrome
- 22. ajax後,Firefox和IE不會工作
- 23. Link在Chrome和Firefox沒有點擊,但在Opera和IE
- 24. IE和Chrome和Firefox之間的Print.css差異
- 25. Firefox和Chrome和IE瀏覽器的CSS3變換區別
- 26. JavaScript工作在IE和Edge上,但不適用於Chrome和Firefox
- 27. 如何在Chrome和IE/Edge中顯示.webp和Firefox中的.jpg?
- 28. Selenium基本身份驗證Tomcat - 適用於Chrome,IE,Firefox的解決方案
- 29. 在FireFox上,但無法在Chrome或IE
- 30. 在firefox/IE/Chrome中製作td相同
指定硒版本,語言及其版本(如java,python)和瀏覽器版本的具體答案。 –