黄瓜(Java)和Selenium:使用setProperty在哪里放置驱动程序路径? [英] Cucumber (Java) and Selenium: where to place driver path using setProperty?

查看:132
本文介绍了黄瓜(Java)和Selenium:使用setProperty在哪里放置驱动程序路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Cucumber for Java和Selenium构建一个测试套件。我的项目结构几乎是这样的:




  • src / test / java:这是我执行测试步骤的地方。

    li>
  • src / test / resources / features:这是我的功能文件。

  • src / test / resources / seleniumdrivers:这是我放置chromedriver的地方.exe。



现在,我要做的是在src / test / java中添加一个Hooks.java类,方法是设置使用@Before挂钩在驱动程序路径上:

  @Before 
public void setUpDriver(){
System.setProperty( webdriver.chrome.driver, src\\测试\\资源\\seleniumdrivers\\chromedriver.exe);
}

但是,由于此方法将在每种情况下运行,因此我想找到一种更好的方法来设置路径,因此只完成了一次。请注意,我希望将驱动程序包含在项目结构中,并使用系统属性对其进行设置(我的意思是,我不想将驱动程序放置在文件系统中的某个位置,并将其添加到PATH环境变量中)。 >

有更好的方法吗?

解决方案

您可以创建一个属性文件,例如 config.properties ,用于存储在执行过程中使用的所有全局值以及 chromedriver.exe 并在所有情况下都先阅读它,并在整个执行过程中使用。

 公共类挂钩{
private static boolean beforeSuit = true;
私有静态字符串executePath;
静态属性道具;

@
之前public void beforeAll(){
if(beforeSuit){
prop = new Properties();
ClassLoader loader = Thread.currentThread()。getContextClassLoader();
InputStream stream = loader.getResourceAsStream( / config.properties);
prop.load(stream);
//您可以在要启动chrome的任何地方使用它。
executePath = prop.getProperty( executablePath);
//使其仅执行一次
beforeSuit = false;

//如果您只希望启动浏览器一次,则可以在此处获取该代码。
}

//在这里您可以让代码在每种情况下都可以执行。

}
}


I'm building a test suite using Cucumber for Java with Selenium. And my project structure is pretty much like this:

  • src/test/java: this is where I have my test steps implementation.
  • src/test/resources/features: this is where I have feature files.
  • src/test/resources/seleniumdrivers: this is where I put chromedriver.exe.

Now, what I did was to add a Hooks.java class in src/test/java with a method that sets up the driver path, using the @Before hook:

@Before
public void setUpDriver(){
    System.setProperty("webdriver.chrome.driver", "src\\test\\resources\\seleniumdrivers\\chromedriver.exe");
}

However, since this method will run before each scenario, I'd like to find a better way to set up the path, so it's only done once. Please note that I want to have the driver within my project structure and set it using a system property (I mean, I don't want to place the driver somewhere in my filesystem and add it to the PATH environment variable).

Is there a better way to do this?

解决方案

You can create one property file like config.properties to store all the global values which you use throughout the execution and also path of the chromedriver.exe and read it before all scenarios and use throughout the executions like this.

public class Hooks {
    private static boolean beforeSuit = true;
    private static String executablePath;
    static Properties prop;

    @Before
    public void beforeAll() {
        if(beforeSuit) {
            prop = new Properties();
            ClassLoader loader = Thread.currentThread().getContextClassLoader();           
            InputStream stream = loader.getResourceAsStream("/config.properties");
            prop.load(stream);
            //You can use this anywhere you want to launch the chrome.
            executablePath = prop.getProperty("executablePath");
            //To make it execute only once
            beforeSuit = false;

            //If you wish to launch browser only once , you can have that code here.
        }

        //Here you can keep code to be execute before each scenario

    }
}

这篇关于黄瓜(Java)和Selenium:使用setProperty在哪里放置驱动程序路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆