eclipse 如何使用 ChromeDriver 在 Chrome 中执行 Selenide

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30768509/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 22:41:31  来源:igfitidea点击:

How can I execute Selenide in Chrome using ChromeDriver

javaeclipseselenium

提问by Afamee

I started using selenide (selenium wrapper api) and must say its a great tool but my only issue is its lack of documentation or usage examples online yet.

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

Any idea how to run your application coded in selenide in google-Chrome. I am using eclipse as IDE. I have added an environment variable "browser" with value chrome in my run configuration but when I run it picks up firefox.

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

My stack is JDBC Java Selenide

我的堆栈是 JDBC Java Selenide

回答by Filipp Shestakov

Try this

尝试这个

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

You can find some documentation here.

您可以在此处找到一些文档。

回答by Khaja Mohammed

The below code will help you launch the Chrome Browser with Selenide, and not with the selenium. It means you don't have to issue a close or quit command at the end of the iteration.

下面的代码将帮助您使用 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!"));
}

}

}

This should help you even if you are running from the command prompt/ terminal, but if you want to exclusively pass on the Chrome from cmd you can use the "browser" parameter as below

即使您从命令提示符/终端运行,这也应该对您有所帮助,但是如果您想从 cmd 专门传递 Chrome,您可以使用“浏览器”参数,如下所示

-Dselenide.browser=chrome

回答by Вассесуарий Пупочкин

You need to tell Selenide what browser to use. That can be done using Configuration properties:

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

import com.codeborne.selenide.Configuration;

public class Tests {

@Before
public void setBrowser() {
    Configuration.browser = "chrome";
}

Remember: your webdriver should be placed on the standard path. For unix/linux it is: /usr/local/bin;If your webdriver is located on a different path or renamed -- you need to set a system property with right path to the webdriver. For example:

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

Windows:

视窗:

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

Linux\Unix:

Linux\Unix:

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

Make sure that your unix / linux chromedriver is executable. After this, you should have a fully working example (in my case, chromedriver is renamed and has version information):

确保您的 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());
    }
}

回答by JohnP2

Another way is to use this command line switch with Maven:

另一种方法是在 Maven 中使用这个命令行开关:

mvn test -P chrome

It requires the Maven profiles in the pom.xml file such as are seen here:

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

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

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

回答by Snehashish Chakraborty

You can use System.setProperty("selenide.browser", "chrome");for running in the chrome browser. If the same test you need to perform in safari just change the chrome to safari. Eg:

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

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