等待一个浏览器完成操作并在 selenium webdriver 中的另一个浏览器中继续

Waiting for one browser to complete action and continue in another browser in selenium webdriver

场景

我有两个应用程序,一个是我的主要下单网站,另一个是优惠券生成网站。在我的下订单场景之间,我必须打开一个新的浏览器 window 并使用全新的用户代理和其他设置并创建优惠券,获取优惠券价值并返回到之前的 window 并应用优惠券.

示例代码:

WebDriver driver2 = new FirefoxDriver();
driver2.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver2.get("URL");
driver.get(URL);
System.out.println(URL);

如果我没理解错的话,你的问题是让第一个webdriver等到第二个完成获取优惠券代码。

为此我的建议是使用线程并让第一个等待第二个完成。

这是您的 Thread class。它将做什么将属于 run() 方法。

class ThreadDemo extends Thread {
    private Thread t;
    private String threadName;
    private String coupon;
    ThreadDemo( String name){
        threadName = name;
        System.out.println("Creating " +  threadName );
    }
    public void run() {
        WebDriver driver2 = new FirefoxDriver();
        //Operations on website
        coupon="Something";

    }
    public String getCoupon(){
        if (coupon==null){
            return "";
        }else{
            return coupon;
        }
    }

    public void start (){
        System.out.println("Starting " +  threadName );
        if (t == null){
            t = new Thread (this, threadName);
            t.start ();
        }
    }

}

现在就叫它:

 ThreadDemo T1 = new ThreadDemo( "Coupon-code");
 T1.start();
 String coupon = "";
 while(coupon.equals("")) //<-This can be tweaked in 
                          //order to consume less memory and be faster
     coupon=T1.getCoupon();

告诉我结果如何!

抱歉,我正在输入 phone,因此您可能需要仔细检查 API。

您可以使用

String coupon = couponDriver.until(ExpectedConditions.presenceOfElementLocated(By.id("couponId")).getText(); // or getValue()  depend on element type

// 使用优惠券价值

继续购物driver

@Rafael Almeida 是的,我尝试了答案并且它正在工作。但是后来一个接一个地使用相同的驱动程序实例更改了实现。所以不需要一个线程等待。无论如何,尝试这种线程更有趣。