简体   繁体   English

获取不同类中的ThreadLocal变量 - Java - Selenium WebDriver

[英]Get ThreadLocal Variable In Different Classes - Java - Selenium WebDriver

How do I get use a ThreadLocal value that was set in a different class? 如何使用在其他类中设置的ThreadLocal值?

Example, I set these variables in class A: 例如,我在A类中设置了这些变量:

ThreadLocal<String> username = new ThreadLocal<String>();
username.set("user");

ThreadLocal<String> password = new ThreadLocal<String>();
password.set("pass");

I know how to "get" them from within the same class by doing: 我知道如何通过以下方式从同一个类中“获取”它们:

String myUsername = username.get();
String myPassword = password.get();

But I can't find anything on how I would use/get them in Class B? 但我找不到任何关于如何在B级中使用/获取它们的内容?

The basic end goal is I am trying to convert my Selenium WebDriver tests to run in parallel. 基本的最终目标是我试图将我的Selenium WebDriver测试转换为并行运行。 These text values will get entered in one class (A), but then I have another class (B) that contains "methods" that get called upon (check database entries, verify on screen data, etc.) that would need to know the data that was entered in class (A). 这些文本值将输入一个类(A),但后来我有另一个类(B),其中包含需要知道的“方法”(检查数据库条目,验证屏幕数据等)在类(A)中输入的数据。

The way I am doing this for non-ThreadLocal variables successfully is like this: 我成功为非ThreadLocal变量执行此操作的方式如下所示:

Set the value in class A: 在A类中设置值:

@Test (priority = 1)
public void enterData() {

    String environment = "QA";

    // Pass environment to stored variables
    StoredVariables newEnv = new StoredVariables();
    newEnv.setEnv(environment);

    driver.enterData;

}

Store it in class B (StoredVariables): 将其存储在B类(StoredVariables)中:

public class StoredVariables {

    private static String environment = "";

    public StoredVariables() {

    }

    public static String getEnv() {return environment;}
    public void setEnv(String s) {environment = s;}

}

Access it from class C: 从C类访问它:

@Test (priority = 1)
public void verifyData() {

    String environment = StoredVariables.getEnv();

}

First of all I am not fan of TestNG. 首先,我不是TestNG的粉丝。 Parallel running tests is also possible with JUnit if you configure Surefire to run them parallel or if you use Parallel runner. 如果您将Surefire配置为并行运行它们或者使用Parallel runner,则JUnit也可以进行并行运行测试。 Consider probably changing to JUnit. 考虑可能改为JUnit。

If you stick to selenium, then you can create a ITestListener implementation. 如果你坚持使用selenium,那么你可以创建一个ITestListener实现。 This class can have a static ThreadLocal. 这个类可以有一个静态的ThreadLocal。 This should store the WebDriver used by the test. 这应该存储测试使用的WebDriver。 You create a static method, which gives back the ThreadLocal value for test classes. 您创建一个静态方法,它返回测试类的ThreadLocal值。 In onTestStart method you can take care of starting the browser, in onTestSuccess you can stop the browser, in onTestFailure you can take screenshot, save HTML and logs, and stop the browser. 在onTestStart方法中你可以负责启动浏览器,在onTestSuccess中你可以停止浏览器,在onTestFailure你可以截取屏幕,保存HTML和日志,并停止浏览器。

Also take care, that you use the PageObject pattern. 还要注意,使用PageObject模式。

Think of a ThreadLocal<T> as a Map<Thread,T> . ThreadLocal<T>视为Map<Thread,T> It's not meant as a way of sharing values between classes, but a way of maintaining different values in each thread. 它不是一种在类之间共享值的方法,而是一种在每个线程中维护不同值的方法。

The way you pass this variable around is exactly the same as you would any other variable. 传递此变量的方式与任何其他变量完全相同。 Usually by returning it somewhere and/or storing it as a field. 通常通过将其返回到某处并/或将其存储为字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在哪里可以获得Java Selenium Webdriver的IWait和IWebDriver类? - Where do I get the IWait and IWebDriver classes for Java Selenium Webdriver? Selenium WebDriver:不同国家的变量 - Selenium WebDriver: Variable for different countries ThreadLocal变量如何将其对象的副本传递给Java中的不同线程? - How ThreadLocal variable passes copy of its object to different Threads in java? Java Selenium中多个类中Webdriver的相同实例 - Same instance of Webdriver in multiple classes in Java Selenium 使用Java中的Selenium Webdriver获取文本 - Get Text with Selenium Webdriver in Java 使用“public ThreadLocal”时如何为我的监听器 class 获取 WebDriver<webdriver> webdriver = 新的 ThreadLocal<webdriver> ();“ 方法</webdriver></webdriver> - how to get WebDriver for my Listener class when using "public ThreadLocal<WebDriver> webdriver = new ThreadLocal<WebDriver>();" method 测试在不同的监视器Selenium WebDriver Java上失败 - Test failed on different monitor selenium webdriver java 有没有办法用By连接变量。 Java中使用Selenium Webdriver - Is there a way, to concatenate a variable with By. for Selenium Webdriver In Java 在Java中循环使用Selenium Webdriver脚本,但值不同 - Looping a selenium webdriver script in java but with different values 使用 Java 中的 Selenium Webdriver 以不同用户身份运行 IE - Running IE as a different user with Selenium Webdriver in Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM