Selenium java Browser session reuse

前端 未结 3 941
囚心锁ツ
囚心锁ツ 2021-01-20 23:08

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 OpenBr         


        
相关标签:
3条回答
  • 2021-01-20 23:30

    One of the simple way which i figured out recently is to make the webdriver as static under class and reuse the same webdriver in any other test case

    For example in testcase1:

    public class globallogin { 
        static WebDriver driver = new ChromeDriver(); 
    } 
    

    Now you can call the same webdriver in test case 2 as:

    public class skucategory {
    
        static globallogin login = new globallogin(); 
    
        public static void main(String[] args) { 
            login.driver.get(<URL>); 
        } 
    }
    
    0 讨论(0)
  • 2021-01-20 23:31

    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,

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

    Actually what I want to do?

    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.

    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;
    }
    

    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).

    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

    Issue - 2: How can i implement a base starting point?

    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.

    For more:

    1. https://github.com/seleniumhq/selenium-google-code-issue-archive/issues/3927
    2. https://sqa.stackexchange.com/questions/1988/selenium-reuse-existing-browser-session-instead-of-opening-new-windows
    0 讨论(0)
  • 2021-01-20 23:46

    You should check with QMetry Automation framework it provides driver management through TestBase. You don't need to create driver instance, just configure in property or xml configuration file and use driver in any class using 'getDriver()' call.

    WebDriver driver = WebDriverTestBase.getDriver();

    It also has different design concepts like TestPage, TestStep, DataBean, Locator Repository, Data-Driven Testing Listeners and many more.

    In order to configure driver, you can set property driver.name outside the code with driver name like

    driver.name=firefoxDriver

    If you want to run on multiple browser in parallel you can provide driver in xml configuration file like:

    <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
    <suite name="QAF-Gherkin-Demo" verbose="0" parallel="methods" thread-count="10">
        <test name="Run_on_FF">
            <parameter name="driver.name" value="firefoxDriver"/>
            <packages>
                <package name="my.pkg"/>
            </packages>
        </test>
        <test name="Run_on_chrome">
            <parameter name="driver.name" value="chromeDriver"/>
            <packages>
                <package name="my.pkg"/>
            </packages>
        </test>
    </suite>
    

    In above example, it will run all test in my.pkg parallel in two different browsers. You don't need to create or quite driver object in your code.

    It also has mechanism to retry if driver creation fail, which is quite useful when you are running test on cloud.

    0 讨论(0)
提交回复
热议问题