维护并重用现有的webdriver浏览器实例 - java [英] Maintain and re-use existing webdriver browser instance - java

查看:251
本文介绍了维护并重用现有的webdriver浏览器实例 - java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上每次我从eclipse运行我的java代码时,webdriver都会启动一个新的ie浏览器并在大多数情况下成功执行我的测试。但是,我有很多测试要运行,webdriver每次启动一个新的浏览器会话都很痛苦。我需要一种方法来重用以前打开过的浏览器;所以webdriver会打开,即第一次,然后第二次,我运行我的eclipse程序,我希望它只是拿起以前的浏览器实例并继续在同一个实例上运行我的测试。这样,我每次运行程序时都不会启动新的浏览器会话。


假设您有100个测试要在eclipse中运行,您点击该运行按钮并且它们都运行,然后在第87次测试时您会收到错误。然后你回到eclipse,修复那个错误,但是你必须从头再次重新运行所有100个测试。


修复第87次测试的错误,然后从第87次测试恢复执行,而不是从头开始重新执行所有测试,即从测试0开始,这将是很好的。 100.
希望,我很清楚,你们可以得到一些帮助,谢谢顺便说一句。



以下是我尝试维护和重新使用webdriver Internet Explorer浏览器实例的尝试:

Basically every time I run my java code from eclipse, webdriver launches a new ie browser and executes my tests successfully for the most part. However, I have a lot of tests to run, and it's a pain that webdriver starts up a new browser session every time. I need a way to re-use a previously opened browser; so webdriver would open ie the first time, then the second time, i run my eclipse program, I want it to simply pick up the previous browser instance and continue to run my tests on that same instance. That way, I am NOT starting up a new browser session every time I run my program.

Say you have 100 tests to run in eclipse, you hit that run button and they all run, then at about the 87th test you get an error. You then go back to eclipse, fix that error, but then you have to re-run all 100 test again from scratch.

It would be nice to fix the error on that 87th test and then resume the execution from that 87th test as opposed to re-executing all tests from scratch, i.e from test 0 all the way to 100. Hopefully, I am clear enough to get some help from you guys, thanks btw.

Here's my attempt below at trying to maintain and re-use a webdriver internet explorer browser instance:

public class demo extends RemoteWebDriver { 

    public static WebDriver driver;
    public Selenium selenium;
    public WebDriverWait wait;
    public String propertyFile;
    String getSessionId;


    public demo() { // constructor


        DesiredCapabilities ieCapabilities = DesiredCapabilities
                .internetExplorer();
        ieCapabilities
                .setCapability(
                        InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,
                        true);
        driver = new InternetExplorerDriver(ieCapabilities);


        this.saveSessionIdToSomeStorage(getSessionId);
        this.startSession(ieCapabilities);
        driver.manage().window().maximize();
    }

    @Override
      protected void startSession(Capabilities desiredCapabilities) {
        String sid = getPreviousSessionIdFromSomeStorage();
        if (sid != null) {
          setSessionId(sid);
          try {
            getCurrentUrl();
          } catch (WebDriverException e) {
            // session is not valid
            sid = null;
          }
        }
        if (sid == null) {
          super.startSession(desiredCapabilities);
          saveSessionIdToSomeStorage(getSessionId().toString());
        }
      }

    private void saveSessionIdToSomeStorage(String session) {
        session=((RemoteWebDriver) driver).getSessionId().toString();
    }

    private String getPreviousSessionIdFromSomeStorage() {
        return getSessionId;
    }
}



我的希望在这里是通过覆盖来自remoteWebdriver的startSession()方法,它会以某种方式检查我已经在ie中打开了一个webdriver浏览器的实例,它将使用该实例,而不是每次我点击运行时重新创建一个新实例日食中的按钮。


我也可以看到,因为我正在从构造函数创建一个新的驱动程序实例,因为构造函数总是先执行,它会自动创建新的驱动程序实例,所以我可能需要以某种方式改变,但不知道如何。


My hope here was that by overriding the startSession() method from remoteWebdriver, it would somehow check that I already had an instance of webdriver browser opened in i.e and it would instead use that instance as opposed to re-creating a new instance everytime I hit that "run" button in eclipse.

I can also see that because I am creating a "new driver instance" from my constructor, since constructor always execute first, it creates that new driver instance automatically, so I might need to alter that somehow, but don't know how.

我是stackoverflow和selenium webdriver的新手,希望有人在这里救命。

I am a newbie on both stackoverflow and with selenium webdriver and hope someone here can help.

谢谢!

推荐答案

回答你的问题:

没有。您无法使用计算机上当前运行的浏览器。你可以使用相同的浏览器进行不同的测试,只要它在同一个执行中。

No. You can't use a browser that is currently running on your computer. You can use the same browser for the different tests, however, as long as it is on the same execution.

然而,听起来你真正的问题是运行100次测试一遍又一遍地。我建议使用测试框架(如TestNG或JUnit)。通过这些,您可以指定要运行的测试(TestNG将生成所有失败的测试的XML文件,因此当您运行它时,它只会执行失败的测试)。

However, it sounds like your real problem is running 100 tests over and over again. I would recommend using a testing framework (like TestNG or JUnit). With these, you can specify which tests you want to run (TestNG will generate an XML file of all of the tests that fail, so when you run it, it will only execute the failed tests).

这篇关于维护并重用现有的webdriver浏览器实例 - java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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