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

Am trying to close the browser after a test pass at any stage in my code but i keep getting error below, What am trying to do in a nut shell is that, if my test passes at any stage, don't continues code execution, don't hang rather, close browser. Please take a closer look at were i have the tearDown(). if the login is successful at that point, then, browser should close after the sleep().

Please, i have checked previous solutions but didnt work for me. Driver.close or quit should work but its not, maybe am doing something wrong..... Thanks for any help in advance.

Exception in thread "main" org.openqa.selenium.remote.SessionNotFoundException: Session ID is null. Using WebDriver after calling quit()? Build info: version: '2.45.0', revision: '32a636c', time: '2015-03-05 22:01:35' System info: host: '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 info: driver.version: RemoteWebDriver at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:125) at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:66) at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:572) at org.openqa.selenium.remote.RemoteWebDriver$RemoteTargetLocator.window(RemoteWebDriver.java:890) at com.independent.helper.testPageNavi.signInFacebook(testPageNavi.java:475) at com.independent.helper.testing.main(testing.java:113)

Joe
  • 147
  • 1
  • 4
  • 17

1 Answers1

3

You are closing the webdriver after one particular test. This is a good approatch but you will need to start a new webdriver for each new test that you want to run.

You have two options. 1. make a new webdriver after each test. 2. navigate back to the begin page so you can start a new test.

I recommend option 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
}
legopiraat
  • 166
  • 1
  • 11
  • Thanks for your response, but can you please give me a sample. Also if i dont call the close(), at the first successfull test pass, i still get the remaining 2 messages in the catch printed out. why is this?. – Joe Jan 12 '16 at 12:17
  • Each try is 1 test? if this is true why aren't you using unit tests or atleast seperate methodes – legopiraat Jan 12 '16 at 12:20
  • ....Yes you're right..... i get you but am using just one method because its a continuous sign in process that opens screens one after the other. Maybe what i should do is use different methods like you suggested and call each method in other methods as a continuous process.... do you get me?. does it make sense to do it that way? so i mean: `method1(){//code} .... method2(){method1() ... continue code} etc.` – Joe Jan 12 '16 at 12:27
  • Yes that makes a lot of sense! Also keep in mind that you don't need to close the webdriver multiple times. Just once when the whole test is completed. – legopiraat Jan 12 '16 at 12:29
  • .....sorry 1 more question, why is my code printing out the other 2 catch statements even when the execution of the first one was successful. Or is my catch wrongly structured? Just for next time – Joe Jan 12 '16 at 12:34
  • 1
    One suggestion would be to structure your tests so they won't become so lengthily. You can even link the tests to be dependent of the previous one, if steps are the same. This would avoid repeating execution of same steps. TestNG allows you to do this more efficiently. E.g.: `@Test public void registerFacebook() {...}` `@Test (dependsOnMethod="registerFacebook()") public void secondSignIng() {...}` – Johnny C Jan 12 '16 at 14:44