Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在一台计算机上同时使用Selenium在2个浏览器上执行操作?_Java_Selenium_Concurrency - Fatal编程技术网

Java 如何在一台计算机上同时使用Selenium在2个浏览器上执行操作?

Java 如何在一台计算机上同时使用Selenium在2个浏览器上执行操作?,java,selenium,concurrency,Java,Selenium,Concurrency,我正在使用Selenium和Java进行黑盒测试。我想打开两个浏览器,同时在这些浏览器上执行一些操作(比如单击按钮,填充一些文本字段…),这样我就可以看到系统将如何响应。我搜索了这个问题,了解了Selenium网格。但它似乎运行在多台机器上,而不是一台机器上 我想知道我的问题是否有简单的解决方案,我不需要安装任何其他工具。我尝试使用线程,但它没有用。我仍然相信使用线程是最好的解决方案。即使线程不能并行执行(由JVM或OS决定何时运行线程),我们也可以使用以下代码在线程之间切换。切换将以毫秒为间隔

我正在使用Selenium和Java进行黑盒测试。我想打开两个浏览器,同时在这些浏览器上执行一些操作(比如单击按钮,填充一些文本字段…),这样我就可以看到系统将如何响应。我搜索了这个问题,了解了Selenium网格。但它似乎运行在多台机器上,而不是一台机器上


我想知道我的问题是否有简单的解决方案,我不需要安装任何其他工具。我尝试使用线程,但它没有用。

我仍然相信使用线程是最好的解决方案。即使线程不能并行执行(由JVM或OS决定何时运行线程),我们也可以使用以下代码在线程之间切换。切换将以毫秒为间隔,看起来几乎是平行的

欢迎反馈可以改进的地方

请查找以下代码:

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class App implements Runnable {

    WebDriver driver;
    String threadName;

    public App(String threadName, WebDriver driver) {
        this.threadName = threadName;
        this.driver = driver;
    }

    public void run() {

        if (threadName.equals("one")) {
            System.out.println("one : " + driver.getTitle());
        } else {
            System.out.println(driver.getTitle());
        }

        if (threadName.equals("one")) {
            System.out.println(driver.getTitle());
        } else {
            System.out.println("two : " + driver.getTitle());
        }

        if (threadName.equals("one")) {
            System.out.println("one one one");
        } else {
            System.out.println("two two two");
        }
    }

    public static void main(String[] args) {
        WebDriver driver1 = new FirefoxDriver();
        driver1.get("https://www.google.com");

        WebDriver driver2 = new FirefoxDriver();
        driver2.get("https://en.wikipedia.com");

        ExecutorService executor = Executors.newFixedThreadPool(50);
        executor.execute(new App("one", driver1));
        executor.execute(new App("two", driver2));
    }
}
输出如下所示(这可能因随机线程切换而有所不同)

一:谷歌
维基百科,免费的百科全书
谷歌
一个
二:维基百科,免费的百科全书
两个


这对我来说是个新闻,我真的不知道java应用程序可以实现某种远程桌面。但是,开箱即用的思考:您是否尝试过使用虚拟机(例如)?如果Selenium只允许您在不同的机器上运行,这将在同一台机器内创建另一个机器环境,您可以在其中运行另一个浏览器。。。你明白我的意思吗?你试过使用TestNg并行运行测试吗?如果你使用selenium进行测试,你应该使用测试框架,比如JUnit或TestNg。这些测试框架支持内置的并行性。尝试使用您选择的测试框架查找并行测试执行。