Selenium Java浏览器会话重用 [英] Selenium java Browser session reuse

查看:96
本文介绍了Selenium Java浏览器会话重用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题如下:如何在不同Java类中的测试中重复使用浏览器会话?我打开了这样的浏览器:

My question is as follows: How can I reuse a browser session for tests that are in different java classes? I have the browser open like this:

public class OpenBrowser {

    public static WebDriver driver;

    @BeforeSuite
    public static void openb() {
        // Create a new instance of the Firefox driver
        driver = new FirefoxDriver();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    }
}

然后我必须使用一些凭据登录网站,并且在登录后我想执行其他测试,所以我不想为每个测试都登录,因为我需要进行很多测试并登录每个将花费十年.例如,流程如下:登录->推送一些组件->添加一些值->生成报告.在此之后,我想在另一个组件上生成其他报告,但是当我尝试使用它时,会给出一个空指针异常.有任何想法吗?:)

Then I have to log into a website using some credentials and after it's logged I want to perform other tests, I do not want to log in for every test because there are a lot of them that I need to make and logging in for each will take a decade. For example a flow looks like : Log In->Push some component->add some values->Generate a report. Than after this i would like to generate other report on another component but when I try to it gives a null pointer exception. Any ideas? :)

推荐答案

在带有WebDriver的 Selenium 2 中,您可以调用

In Selenium 2 with WebDriver you can call

driver = new FirefoxDriver();

会生成一个浏览器,并且该浏览器将在测试期间保持打开状态,

which spawns a browser, and that browser will stay open for the duration of your testing,

,或者您可以选择使用 driver.Quit()关闭它.

or you can choose to close it with driver.Quit().

我实际上想做什么?

我想在两次测试之间关闭浏览器窗口,以使我知道测试不会脏" ,因为存储的会话数据可能会影响测试的运行方式,但我可以看到其中的价值有针对性的测试,我想尝试几个不同的场景,同时保持相同的会话进行.

I like to close my browser window between tests so that I know my tests aren't "dirty" with stored session data that could affect how the tests run, but I can see value in some targeted tests, where I want to try a couple different scenarios while keeping the same session going.

对于代码级别:您可以检查它是否为空.如果为null,则调用浏览器以提供另一个.

For code level: you can check is it null or not. If null then call the browser to give another.

/**
 * Driver for web application.
 * 
 * @return driver Browser
 * @throws IOException
 */
public WebDriver getDriverBrowser() throws IOException {

if (driverBrowser == null) {
    String sBrowser = PropertyLoader.loadProperty("browser");
    driverBrowser = getBrowser(sBrowser);
    driverBrowser
            .manage()
            .timeouts()
            .implicitlyWait(
                    Integer.valueOf(PropertyLoader
                            .loadProperty("implicit_timeout_sec")),
                    TimeUnit.SECONDS);
    driverBrowser.manage().window().maximize();
}
return driverBrowser;
}

或者您可以使用xml配置将某些测试用例用作类级别或特定于测试的测试.使用TestNG,您可以指定要运行的测试( TestNG将生成所有失败的测试的XML文件,因此在您运行它时,它只会执行失败的测试).

Or you can use xml configuration to use some test cases as class level or test specific.Using TestNG, 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).

问题-1:我的意思是我不想再次登录以继续测试,我不介意如果它打开另一个窗口,实际上正如您所说,它更可靠,但如果我必须再次登录则无济于事.

Issue - 1: I mean i don't want to log in again to continue testing , I don't mind if it opens another window, in fact as you said it's more reliable but not helpful if i have to log in again.

如果您不想登录,那么在每个测试用例中,都需要一个基本的起点.结束任何测试用例后,它将转到基准位置,下一个测试用例将从基准开始

If you don't want to login, then in every testcase, you need a base starting point. After ending any test case, it will go to base position and next testcase will start from the base

问题-2:如何实现基本起点?

由您决定.首先,您必须分析测试用例,然后必须选择哪个点可以作为所有或某些测试用例集的起点.那你就可以做到.

It is up to you. First you have to analyze the test cases, then you have to select which point can be the starting point for all or set of some test cases. Then you can do it.

更多:

  1. https://github.com/seleniumhq/selenium-google-code-issue-archive/issues/3927
  2. https://sqa.stackexchange.com/questions/1988/selenium-reuse-existing-browser-session-in-of-opening-new-windows

这篇关于Selenium Java浏览器会话重用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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