2016-01-12 322 views
0
public void tearDown() 
{  
     driver.quit(); 
} 

public void signInFacebook(String link) throws InterruptedException { 
    log.header("USER SIGN IN VIA FACEBOOK"); 
    log.step("Click the Sign In Link"); 
    clickLink(link); 
    Thread.sleep(3000); 
    log.log("click success......."); 
    log.step("Click 'Sign in with Facebook' Link "); 

    // Store the current window handle 
    String parentHandle = driver.getWindowHandle(); 

    // Perform the click operation that opens new window 
    driver.findElement(By.cssSelector(CONSTANTs.FB_SIGN_IN)).click(); 
    Thread.sleep(5000); 

    // Switch to new window opened 
    log.divider("Facebook Login Screen.....Enter Details Below"); 
    for (String childHandle : driver.getWindowHandles()) { 
     driver.switchTo().window(childHandle); 

     if (!childHandle.equals(parentHandle)) { 
      log.step("Enter Email address"); 
      driver.findElement(By.cssSelector(CONSTANTs.FB_EMAIL_TXTBOX)) 
        .sendKeys(CONSTANTs.EMAIL_ADDRESS); 
      Thread.sleep(2000); 
      log.step("Enter password"); 
      driver.findElement(By.cssSelector(CONSTANTs.FB_PASSWORD_TXTBOX)) 
        .sendKeys(CONSTANTs.FB_PASSWORD); 
      Thread.sleep(2000); 
      log.step("Click Log_In button"); 
      driver.findElement(By.cssSelector(CONSTANTs.LOG_IN)).click(); 
      Thread.sleep(2000); 
      log.log("Check for First Sign In or Return User.......wait"); 
      WebDriver switchPage = driver.switchTo().window(parentHandle); 
      try { 
       if (switchPage.getWindowHandle().contains(parentHandle)) { 
        driver.switchTo().window(childHandle); 
        log.log("Indo App NOT installed..... Click Ok to accept FB permissions..."); 
        optionalClick(By.cssSelector(CONSTANTs.OK_CONTINUE_AS)); 
        driver.switchTo().window(parentHandle); 
       } 
       log.log("DONE....... "); 
       // break; 
      } catch (Exception e) { 
       log.log("Indo App Already exist on Users Fb acct ... Move to next screen"); 
      } 
      break; 
     } 
    } 

    log.log("Check if User is Signed In or Has Signed In with another account.......wait"); 
    Thread.sleep(5000); 
    try { 
     if ((driver.findElement(By.cssSelector(CONSTANTs.MY_ACCOUNT_LINK))) 
       .isDisplayed()) { 
      log.log("First Sign In ....... Successfully Logged in Using FB."); 
      Thread.sleep(3000); 
      tearDown(); 
     } 
    } catch (Exception e) { 
     log.header2("Second Sign In ...... User Already signed In with a different Acct........ pls wait"); 
    } 
    driver.switchTo().window(parentHandle);  
    Thread.sleep(3000); 

    try { 
     if (driver.findElement(
       By.cssSelector(CONSTANTs.ALREADY_SIGNED_UP_PASSWD_TXTBOX)) 
       .isDisplayed()) { 
      log.log("User Already signed Up with a NATIVE acct .....Please Enter Password for Native acct"); 
      driver.findElement(
        By.cssSelector(CONSTANTs.ALREADY_SIGNED_UP_PASSWD_TXTBOX)) 
        .sendKeys(CONSTANTs.NATIVE_PASSWORD); 
      Thread.sleep(2000); 
      driver.findElement(
        By.cssSelector(CONSTANTs.ALREADY_SIGNED_UP_BTN)) 
        .click(); 
      Thread.sleep(2000); 
      log.log("Successfully Linked NATIVE acct with FB."); 
      Thread.sleep(3000); 
      tearDown(); 
     } 

    } catch (Exception e1) { 
     log.log(" acct....wait, checking other acct......"); 
    } 


    try{ 
     if (driver.findElement(
       By.cssSelector(CONSTANTs.SIGN_IN_WITH_GOOGLE_BTN)) 
       .isDisplayed()) { 
      log.log("User is Signed in Already with GOOGLE account ... Click Google button to Link account."); 
      Thread.sleep(3000); 
      driver.findElement(
        By.cssSelector(CONSTANTs.SIGN_IN_WITH_GOOGLE_BTN)) 
        .click(); 
      Thread.sleep(2000); 
      log.log("Successfully Linked ."); 
      Thread.sleep(3000); 
      tearDown(); 
     } 
    } 
     catch(Exception e2) { 
      log.log("No Google acct present either.... Please contact Gigya customer support...."); 
      Thread.sleep(3000); 
      tearDown(); 
     } 
} 

我試圖測試合格後的任何階段,在我的代碼關閉瀏覽器,但我一直在下面得到錯誤,什麼我試圖在堅果殼裏做的是,如果我的測試在任何階段通過,不要繼續執行代碼,不要掛斷,關閉瀏覽器。請仔細看看是否有tearDown()。如果登錄在那一點成功,那麼瀏覽器應該在sleep()後關閉。關閉瀏覽器

請檢查以前的解決方案,但沒有爲我工作。 Driver.close或退出應該工作,但它沒有,也許我做錯了什麼..... 感謝您的任何幫助提前。

線程「main」中的異常org.openqa.selenium.remote.SessionNotFoundException:會話標識爲null。調用quit()後使用WebDriver? 構建信息:版本:'2.45.0',修訂:'32a636c',時間:'2015-03-05 22:01:35' 系統信息:主機:'Ola-PC',ip:'10 .255.253.14 ',os.name:'Windows 7',os.arch:'amd64',os.version:'6.1',java.version:'1.7.0_76' 驅動程序信息:driver.version:RemoteWebDriver at org.openqa .selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:125) 在org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:66) 在org.openqa.selenium.remote.RemoteWebDriver.execute在C(RemoteWebDriver.java:572) 在org.openqa.selenium.remote.RemoteWebDriver $ RemoteTargetLocator.window(RemoteWebDriver.java:890) 在com.independent.helper.testPageNavi.signInFacebook(testPageNavi.java:475) om.independent.helper.testing.main(testing.java:113)

回答

2

您正在關閉一個特定測試後的webdriver。這是一個很好的解決方案,但是您需要爲每個要運行的新測試啓動一個新的webdriver。

您有兩種選擇。 1.每次測試後新建一個webdriver。 2.導航回到開始頁面,以便開始新的測試。

我建議選擇1

@Before 
public void before() { 
    driver = new ChromeDriver(); 
} 

@After 
public void tearDown() 
{ 
    driver.quit(); 
} 

@Test 
public void signInFacebook(String link) throws InterruptedException { 
    //Do 1 test here 
} 
+0

感謝您的答覆,但可以請你給我一個樣品。此外,如果我不打電話close(),在第一次成功的測試通過,我仍然得到其餘2封信息打印出來。爲什麼是這樣?。 – Joe

+0

每次嘗試是1測試?如果這是真的你爲什麼不使用單元測試或至少單獨的方法 – legopiraat

+0

....是的你是對的.....我得到你但只使用一種方法,因爲它是一個連續的登錄過程,打開屏幕一個接一個地。也許我應該做的是使用不同的方法,像你所建議的,並將其他方法中的每個方法作爲一個連續的過程調用....你能找到我嗎?這樣做有意義嗎?所以我的意思是:'method1(){// code} .... method2(){method1()...繼續代碼} etc.' – Joe