如何使用ChromeDriver在Chrome中执行Selenide

我开始使用selenide(selenium wrapper api)并且必须说它是一个很好的工具,但我唯一的问题是它缺乏在线文档或用法示例。

知道如何在google-Chrome中运行以selenide编码的应用程序。 我使用eclipse作为IDE。 我在运行配置中添加了一个带有值chrome的环境变量“browser”但是当我运行它时会选择firefox。

我的堆栈是JDBC Java Selenide

试试这个

 System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver"); System.setProperty("selenide.browser", "Chrome"); open("http://google.com"); 

你可以在这里找到一些文档。

以下代码将帮助您启动使用Selenide的Chrome浏览器,而不是使用selenium。 这意味着您不必在迭代结束时发出关闭或退出命令。

 import static com.codeborne.selenide.CollectionCondition.size; import static com.codeborne.selenide.Condition.text; import static com.codeborne.selenide.Selenide.*; import org.junit.Rule; import org.junit.Test; import org.openqa.selenium.By; import com.codeborne.selenide.junit.ScreenShooter; public class SimpleDateFormatClass { @Rule public ScreenShooter takeScreenshotSelenide = ScreenShooter.failedTests().succeededTests(); @Test public void checkGoogleSearchResultsReturnValidResultNumberAndText() { System.setProperty("webdriver.chrome.driver", "/usr/bin/chromedriver_2"); //Doesn't matter chrome or Chrome as this is case insensitive. System.setProperty("selenide.browser", "Chrome"); open("http://google.com"); $(By.name("q")).setValue("Selenide").pressEnter(); // assure there are 10 results in the page // static import shortcut, place the cursor on the method and press // ctrl+shift+m/ cmd+shift+m // $ -> driver.findElement, $$ -> driver.findElements $$(".iris li.g").shouldHave(size(10)); $(".iris li.g").shouldHave(text("Selenide World!")); } 

}

即使您从命令提示符/终端运行,这也可以帮助您,但如果您想从cmd专门传递Chrome,则可以使用“browser”参数,如下所示

 -Dselenide.browser=chrome 

你需要告诉Selenide使用什么浏览器。 这可以使用配置属性完成:

 import com.codeborne.selenide.Configuration; public class Tests { @Before public void setBrowser() { Configuration.browser = "chrome"; } 

请记住:您的webdriver应该放在标准路径上。 对于unix / linux,它是: /usr/local/bin; 如果您的webdriver位于不同的路径上或重命名 – 您需要设置一个系统属性,其路径为webdriver。 例如:

视窗:

 System.setProperty("webdriver.chrome.driver", "C:\\Program files\\chromedriver.exe"); 

Linux的\ Unix的:

 System.setProperty("webdriver.chrome.driver","/usr/share/chromedriver"); 

确保你的unix / linux chromedriver是可执行的。 在此之后,你应该有一个完整的工作示例(在我的例子中,chromedriver被重命名并具有版本信息):

 import com.codeborne.selenide.*; import org.openqa.selenium.*; import org.junit.*; import static com.codeborne.selenide.Selenide.$; import static com.codeborne.selenide.Selenide.open; import static com.codeborne.selenide.WebDriverRunner.getWebDriver; public class TestClass { @Before public void setBrowser(){ Configuration.browser = "chrome"; Configuration.browserSize = "1920x1080"; Configuration.holdBrowserOpen = true; System.setProperty("webdriver.chrome.driver", "/usr/local/bin/chromedriver_2.33"); } @Test public void gotoGoogle(){ open("https://www.google.com"); WebElement searchBox = $(By.xpath("//input[@id='lst-ib']")); $(searchBox).shouldBe(Condition.visible).setValue("How can I execute Selenide in Chrome using ChromeDriver").pressEnter(); WebElement firstResultLink = $(By.xpath("(//div[@class='rc']//a)[1]")); $(firstResultLink).click(); System.out.println(getWebDriver().getCurrentUrl()); } } 

另一种方法是使用Maven的命令行开关:

 mvn test -P chrome 

它需要pom.xml文件中的Maven配置文件,如下所示:

https://github.com/selenide-examples/google

您可以使用System.setProperty("selenide.browser", "chrome"); 在Chrome浏览器中运行。 如果您需要在safari中执行相同的测试,只需将chrome更改为safari。 例如:

 System.setProperty("selenide.browser", "safari"); open("http://idemo.bspb.ru/");