即使在添加cookie后也要从新标签中注销

时间:2019-02-01 20:08:43

标签: selenium cookies webdriver session-cookies

java.lang.AssertionError:测试失败,预期为[200],但发现为[302] 预期:200 实际:302

我收到重定向错误/从新标签页注销

WelcomePage welcomePage = psloginPage.clickLogInButton();

    ExaminationReportsPage examinationReportsPage = welcomePage.clickReportsTab();

    examinationReportsPage.reportDropDown("Portion Pass Rate (First Time) Cumulative");
    examinationReportsPage.startDateDropDown("01 - January", "02", "2017");
    examinationReportsPage.endDateDropDown("01 - January", "08", "2017");
    String currentPageHandle = driver.getWindowHandle();
    Set<Cookie> cookiesInstance1 = driver.manage().getCookies();
    examinationReportsPage.clickSubmitButton();

    ArrayList<String> tabHandles = new ArrayList<String>(driver.getWindowHandles());
    for (String tab : tabHandles) {
        if (!tab.equals(currentPageHandle)) {
            driver.switchTo().window(tab);
            Thread.sleep(5000);

            String currentUrl = driver.getCurrentUrl();
            try {
                URL url = new URL(currentUrl);
                try
                {
                    for(Cookie cookie:cookiesInstance1)
                    {
                        System.err.println(cookie);
                        driver.manage().addCookie(cookie);
                    }
                }
                catch(Throwable e)
                {
                    System.err.println("Error While setting Cookies: " + e.getMessage());
                }
                HttpURLConnection objhttpURLConnection = (HttpURLConnection) url.openConnection();

                Map<String, List<String>> map = objhttpURLConnection.getHeaderFields();

                objhttpURLConnection.setConnectTimeout(10000);
                objhttpURLConnection.connect();

                //String state=((JavascriptExecutor) driver).executeScript("return document.readyState").toString();
               for (Map.Entry<String, List<String>> entry : map.entrySet()) {
                    System.out.println("Key : " + entry.getKey()+ " ,Value : " + entry.getValue());
                }

                if (objhttpURLConnection.getResponseCode() != 200) {
                    System.out.println(currentUrl + " Error*-* +" + objhttpURLConnection.getResponseCode());
                    System.out.println(objhttpURLConnection.getResponseMessage());
                    Assert.assertEquals(objhttpURLConnection.getResponseCode(),200,"Test Failed");
                }else
                {
                    System.out.println(currentUrl + " Perfect*-* " + objhttpURLConnection.getResponseCode());
                    System.out.println(currentUrl + " *-* " + objhttpURLConnection.getResponseMessage());
                    Assert.assertEquals(objhttpURLConnection.getResponseCode(),200,"Test Pass");
                }
            } catch (Exception e) {
            }
        }
    }

即使添加了cookie,也要从新标签中注销。

0 个答案:

没有答案