【问题标题】:How to open an browser using the same session in Selenium Webdriver?如何在 Selenium Webdriver 中使用相同的会话打开浏览器?
【发布时间】:2016-08-02 23:15:36
【问题描述】:

我有一个应用程序自动化框架,其中应用程序实际上被视为浏览器。现在,这是我的功能。

通常它总是启动登录页面。现在我的测试用例要求我先使应用程序崩溃,然后重新启动它,这将指向一些“错误报告”页面,而不是这次启动登录页面。

如果我从命令提示符执行此操作,则此功能可以正常工作,因为每个命令提示符窗口都是一个会话,所以基本上它会在同一个会话中崩溃并重新打开。

现在这在通过 webdriver 运行时不起作用,因为每次 @BeforeClass 运行时,它都会创建应用程序的一个新实例,因此 i 再次定向到登录页面而不是定向到错误页面。

任何有关如何在同一会话中实现应用程序崩溃和重新启动的建议都会有很大帮助。我只提供了调用驱动程序的一小部分代码。

protected void setupChromeRemoteDriver(String hubUrl, String platformName) throws IOException {

        Platform platform = (platformName != null) ? Platform.valueOf(platformName) : Platform.ANY;

        DesiredCapabilities capabilities = DesiredCapabilities.chrome();
        capabilities.setBrowserName("chrome");
        capabilities.setPlatform(platform);
        capabilities.setCapability(CapabilityType.TAKES_SCREENSHOT, true);

        Set<Cookie> cookies = driver.manage().getCookies();
        for(Cookie cookie : cookies){
            driver.manage().addCookie(cookie);
        }

        driver = new CustomRemoteWebDriver(new URL(hubUrl), capabilities);
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(Constants.ELEMENT_TIMEOUT_SECONDS, TimeUnit.SECONDS);
    }

CrashApplication.java

public void crashUTAApp(){
        String filePath = Constants.CRASHAPP_BATCHFILE_LOCATION;
        try{
            Process process = Runtime.getRuntime().exec(filePath); 
            if(null != process && process.waitFor() == 0 && process.exitValue() == 0){
                Reporter.log("App Crashed");
            }else{
                Reporter.log("Failed to crash app");
            }
        }catch(Exception e){
            e.printStackTrace();
        }
    } 

【问题讨论】:

    标签: java session selenium


    【解决方案1】:

    实现此目的的一种可能解决方法是在测试 junit 类中使用 @Before 注释并从其内部调用 CrashApplication。这样,您将使用@BeforeClass 启动驱动程序并在@Before 内部执行崩溃。请注意,如果您使用此方法,则整个类中应仅涵盖此场景,因为 @Before 在每个 @Test 之前执行。 结构的简单表示:

    (测试类)

    @BeforeClass(新建app实例并调用webDriver实例)

    @Before(在每个@Test之前执行,调用CrashApplication作为真正@Test的前提)

    @Test(将使用相同的会话执行并导航到登录屏幕)

    【讨论】:

      猜你喜欢
      • 2013-11-27
      • 2014-08-01
      • 1970-01-01
      • 2017-01-13
      • 2012-04-08
      • 2016-06-03
      • 1970-01-01
      • 1970-01-01
      • 2017-10-23
      相关资源
      最近更新 更多