Selenium WebDriver - 在测试期间删除cookie并重新启动浏览器

时间:2015-05-11 08:50:24

标签: java selenium cookies selenium-webdriver testng

我有一个测试用例,测试是删除cookie后重新启动浏览器

后仍然存在购物车项目
  1. 将商品添加到购物车(将会显示已指定的商品数量)
  2. Delete the cookies在浏览器上,
  3. Close the browser
  4. Relaunch the browser
  5. 检查是否仍然存在购物车计数项目
  6. 我的主要代码如下。

    // 1. Add items to shopping cart
    // code go here
    
    // 2. Clear the cookies on the browser
    driver.manage().deleteAllCookies();         
    driver.navigate().refresh();
    // 3. Close the browser
    driver.close();                             
    
    ProfilesIni firProfiles = new ProfilesIni();        
    FirefoxProfile wbdrverprofile = firProfiles.getProfile("default");  
    // 4. Relaunch the browser      
    driver = new FirefoxDriver(wbdrverprofile);                 
    driver.get("http://www.google.com");        
    
    Boolean isCartCountExists = driver.findElements(By.id(cartCount)).size()!=0;
    if(isCartCountExists == true){
        System.out.println("Test Failed.");
    }else{
        System.out.println("Test Passed.")
    }
    

    @AfterTest代码

    @AfterTest
        public void closeBrowser(){
    
            driver.close();
            ExistingmozillaBrowser=null;
    
        }
    

    获取下面的错误日志

    [TestNG] Running:
    
    FAILED CONFIGURATION: @AfterTest closeBrowser
    org.openqa.selenium.remote.UnreachableBrowserException: Error communicating with the remote browser. It may have died.
    Build info: version: '2.44.0', revision: '76d78cf', time: '2014-10-23 20:03:00'
    System info: host: 'C111400215M4232', ip: '192.168.0.11', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_45'
    Driver info: driver.version: RemoteWebDriver
        at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:593)
        at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:614)
        at org.openqa.selenium.remote.RemoteWebDriver.close(RemoteWebDriver.java:463)
        at testCases.SuiteBase.closeWebBrowser(SuiteBase.java:380)
        at testCases.ModuleOne.myTestCase01.closeBrowser(myTestCase01.java:387)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
        at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:564)
        at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:213)
        at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:138)
        at org.testng.TestRunner.afterRun(TestRunner.java:1014)
        at org.testng.TestRunner.run(TestRunner.java:621)
        at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
        at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
        at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
        at org.testng.SuiteRunner.run(SuiteRunner.java:240)
        at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
        at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
        at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
        at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
        at org.testng.TestNG.run(TestNG.java:1057)
        at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
        at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
        at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)
    Caused by: org.apache.http.conn.HttpHostConnectException: Connect to 127.0.0.1:7056 [/127.0.0.1] failed: Connection refused: connect
        at org.apache.http.impl.conn.HttpClientConnectionOperator.connect(HttpClientConnectionOperator.java:140)
        at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:318)
        at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:363)
        at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:219)
        at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:195)
        at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:86)
        at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:108)
        at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
        at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:72)
        at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:57)
        at org.openqa.selenium.remote.HttpCommandExecutor.fallBackExecute(HttpCommandExecutor.java:215)
        at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:184)
        at org.openqa.selenium.firefox.internal.NewProfileExtensionConnection.execute(NewProfileExtensionConnection.java:165)
        at org.openqa.selenium.firefox.FirefoxDriver$LazyCommandExecutor.execute(FirefoxDriver.java:362)
        at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:572)
        ... 26 more
    Caused by: java.net.ConnectException: Connection refused: connect
        at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
        at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
        at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
        at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
        at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
        at java.net.PlainSocketImpl.connect(Unknown Source)
        at java.net.SocksSocketImpl.connect(Unknown Source)
        at java.net.Socket.connect(Unknown Source)
        at org.apache.http.conn.socket.PlainConnectionSocketFactory.connectSocket(PlainConnectionSocketFactory.java:72)
        at org.apache.http.impl.conn.HttpClientConnectionOperator.connect(HttpClientConnectionOperator.java:123)
        ... 40 more
    
    FAILED: myTestCase01Test("myTestCase01", "pc", "laptop", "2", "usd")
    org.openqa.selenium.remote.UnreachableBrowserException: Error communicating with the remote browser. It may have died.
    Build info: version: '2.44.0', revision: '76d78cf', time: '2014-10-23 20:03:00'
    System info: host: 'C111400215M4232', ip: '192.168.0.11', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_45'
    Driver info: driver.version: RemoteWebDriver
        at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:593)
        at org.openqa.selenium.remote.RemoteWebDriver.findElements(RemoteWebDriver.java:377)
        at org.openqa.selenium.remote.RemoteWebDriver.findElementsByXPath(RemoteWebDriver.java:453)
        at org.openqa.selenium.By$ByXPath.findElements(By.java:352)
        at org.openqa.selenium.remote.RemoteWebDriver.findElements(RemoteWebDriver.java:340)
        at pageObjects.HomePage.check_noCartCount(HomePage.java:550)
        at testCases.ModuleOne.myTestCase01.myTestCase01Test(myTestCase01.java:317)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
        at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
        at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
        at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
        at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
        at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
        at org.testng.TestRunner.privateRun(TestRunner.java:767)
        at org.testng.TestRunner.run(TestRunner.java:617)
        at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
        at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
        at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
        at org.testng.SuiteRunner.run(SuiteRunner.java:240)
        at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
        at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
        at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
        at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
        at org.testng.TestNG.run(TestNG.java:1057)
        at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
        at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
        at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)
    Caused by: org.apache.http.conn.HttpHostConnectException: Connect to 127.0.0.1:7056 [/127.0.0.1] failed: Connection refused: connect
        at org.apache.http.impl.conn.HttpClientConnectionOperator.connect(HttpClientConnectionOperator.java:140)
        at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:318)
        at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:363)
        at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:219)
        at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:195)
        at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:86)
        at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:108)
        at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
        at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:72)
        at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:57)
        at org.openqa.selenium.remote.HttpCommandExecutor.fallBackExecute(HttpCommandExecutor.java:215)
        at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:184)
        at org.openqa.selenium.firefox.internal.NewProfileExtensionConnection.execute(NewProfileExtensionConnection.java:165)
        at org.openqa.selenium.firefox.FirefoxDriver$LazyCommandExecutor.execute(FirefoxDriver.java:362)
        at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:572)
        ... 30 more
    Caused by: java.net.ConnectException: Connection refused: connect
        at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
        at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
        at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
        at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
        at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
        at java.net.PlainSocketImpl.connect(Unknown Source)
        at java.net.SocksSocketImpl.connect(Unknown Source)
        at java.net.Socket.connect(Unknown Source)
        at org.apache.http.conn.socket.PlainConnectionSocketFactory.connectSocket(PlainConnectionSocketFactory.java:72)
        at org.apache.http.impl.conn.HttpClientConnectionOperator.connect(HttpClientConnectionOperator.java:123)
        ... 44 more
    
    
    ===============================================
        Default test
        Tests run: 1, Failures: 1, Skips: 0
        Configuration Failures: 1, Skips: 0
    ===============================================
    
    
    ===============================================
    Default suite
    Total tests run: 1, Failures: 1, Skips: 0
    Configuration Failures: 1, Skips: 0
    ===============================================
    
    似乎关闭浏览器后,重启后测试无法继续。任何帮助将不胜感激。感谢。

2 个答案:

答案 0 :(得分:3)

我认为这种方式应该有效:

步骤(这是一个chrome示例):

  • 在浏览器配置中使用user-dir参数:

    System.setProperty("webdriver.chrome.driver", "res/chromedriver.exe");
    DesiredCapabilities capabilities = DesiredCapabilities.chrome();
    ChromeOptions options = new ChromeOptions();
    options.addArguments("test-type");
    options.addArguments("start-maximized");
    options.addArguments("user-data-dir=D:/temp/");
    capabilities.setCapability("chrome.binary","res/chromedriver.exe");
    capabilities.setCapability(ChromeOptions.CAPABILITY,options);
    WebDriver driver = new ChromeDriver(capabilities);
    
  • 将商品添加到购物车

  • 关闭驱动程序(不要退出)

    driver.close();
    
  • 在后台delete user-dir D:/temp/,其中包含浏览器个人资料。这将是cookie删除部分。

  • 重新初始化驱动程序并验证购物车是否为空

    driver = new ChromeDriver(capabilities);
    
PS:我希望这会有所帮助。我已经看到这种类型的代码可用于测试"记住我"功能。购物车我不太确定。

答案 1 :(得分:0)

Java:

import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.WebDriver;


private static WebDriver driver;

public static void restartDriver() {
    driver.manage().deleteAllCookies();         // Clear Cookies on the browser
    driver.quit();
    driver = null;
    driver = new ChromeDriver();

}

这是开始会话的最短方法。