我开始使用硒化物(selenium wrapper api)并且必须说它是一个很好的工具,但我唯一的问题是它缺少在线文档或用法示例。
知道如何在google-Chrome中运行以硒化物编码的应用程序。我使用eclipse作为IDE。我在运行配置中添加了一个带有值chrome的环境变量“browser”,但是当我运行它时会选择firefox。
我的筹码是 JDBC Java的 硒
答案 0 :(得分:5)
以下代码将帮助您使用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,则可以使用"浏览器"参数如下
-Dselenide.browser=chrome
答案 1 :(得分:4)
试试这个
System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
System.setProperty("selenide.browser", "Chrome");
open("http://google.com");
您可以找到一些文档here。
答案 2 :(得分:4)
您需要告诉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());
}
}
答案 3 :(得分:0)
另一种方法是将此命令行开关与Maven一起使用:
mvn test -P chrome
它需要pom.xml文件中的Maven配置文件,如下所示:
答案 4 :(得分:0)
您可以使用System.setProperty("selenide.browser", "chrome");
在Chrome浏览器中运行。如果你需要在safari中执行相同的测试,只需将chrome更改为safari。
例如:
System.setProperty("selenide.browser", "safari"); open("http://idemo.bspb.ru/");