2017-09-01 67 views
2

我想验证,如果硒-chromedriver可以共享多个webdriver实例之间的cookie。这个想法是,我将创建一个webdriver实例并登录到应用程序中。我将保持此webdriver实例运行,并将创建另一个webdriver实例并尝试访问同一站点上的安全页面。由于我已经从第一个实例登录到应用程序,因此我应该自动登录到第二个实例。但是这不起作用。经过大量研究后,我发现我需要设置一个指定Chrome创建会话cookie的目录,同时创建一个chromedriver实例。以下是我的代码。硒与ChromeDriver和使用Chrome配置文件

public class TestClass { 

    private static WebDriver webDriver = null; 

    public static void main(String[] args) throws InterruptedException { 

     TestClass tc = new TestClass(); 

     if(webDriver == null) { 

      webDriver = tc.getWebDriverInstance(); 

      webDriver.get("https://example.com/loginpage"); 
      //enter userid/password, click login button 
      //login is successful and redirected to next page - https://example.com/securepage 
     } 

     WebDriver newWebDriverOne = this.getWebDriverInstance(); 
     newWebDriverOne.get("https://example.com/securepage"); // this doesn't work 

     WebDriver newWebDriverTwo = this.getWebDriverInstance(); 
     newWebDriverTwo.get("https://example.com/securepage");// this doesn't work 
    } 

    WebDriver getWebDriverInstance(){  

    DesiredCapabilities dCaps = DesiredCapabilities.chrome();  
    ChromeOptions options = new ChromeOptions();  
    options.addArguments("user-data-dir=C:/user/me/selenium/chrome");  
    dCaps.setCapability(ChromeOptions.CAPABILITY, options); 
    return new ChromeDriver(dCaps); 
    } 
} 

问题是,当我在第一个之后创建的newWebDriver实例上调用get()时,没有任何反应。自从我创建一个新的webdriver实例后,Selenium打开第二个窗口,但get()不执行任何操作。我试图在打开的窗口中手动输入securepage url,它工作。我能够看到安全页面,而无需重定向到登录页面。

如果我们指定了user-data-dir,好像不可能有多个webdriver实例。是否有其他选项可以在会话之间共享Cookie数据?

-----更新------ 我试图做到这一点的原因是一个非常奇怪的用例。

- >我需要同时运行多个自动化运行 - 只有我知道要实现这一点是创建多个webdriver实例。

- >自动化脚本需要仅使用一个帐户登录。而IDP一次只允许一个活动会话。这意味着,如果自动化脚本登录第二个webdriver实例,则第一个webdriver实例将注销。

因此,当研究在webdriver实例之间共享会话的方式时,我遇到了user-data-dir选项。

+0

默认情况下,webdriver的每个新实例都会创建一个不与其他实例共享的新临时配置文件。那么验证它可以在多个实例之间共享Cookie的知识点是什么?它看起来像你试图测试驱动程序,而不是一个网站。 –

+0

我没有试图测试webdriver ..我试图解决一个用例,并想知道是否有一种方法可以通过Selenium/WebDriver来实现。 – RKodakandla

+0

我引用:'试图验证,如果selenium-chromedriver可以'。您可能想要重新制定。 –

回答

0

难道你不能只使用一个Chrome配置文件,并将cookie保存在一个配置文件中,它会自动登录这些细节,因为它记得你吗?我不明白你为什么需要两个实例。

如果您想使用多个实例,我假设您必须在单独的实例中加载完全相同的配置文件,因为新的配置文件不起作用。

+0

我已将更新添加到我的问题。我的用例驱动需要多个webdriver实例。他们需要共享会话,以便用户不必分别在每个实例上登录 - 并且多个同时登录不起作用...当发生新的登录时,IDP会终止上一个活动会话 – RKodakandla