0

I have a session timeout problem like this:

I am using selenium + webdriver + java(junit)

While testing each(tab) test case of particular web page in one programme, i need first test Login button, after successfully login i need to click on button called "add device". Now my problem is when i click on "add device" Login page comes and says "session time out". Please help me...........Thanks in advance......

And Updated code is:

import java.io.File;
import java.util.List;
import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.internal.ProfilesIni;
import org.openqa.selenium.ie.InternetExplorerDriver;
//import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.Select;
import com.thoughtworks.selenium.SeleneseTestBase;

public class NuemeraClass extends SeleneseTestBase {
  private static WebDriver driver;
  private String baseUrl;
  private boolean acceptNextAlert = true;
  private StringBuffer verificationErrors = new StringBuffer();
  String str = null;
  private boolean check = false;

  @Before
  public void setUp() throws Exception {

    // System.setProperty("webdriver.chrome.driver","C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe");   
     //driver = new ChromeDriver();
     // DesiredCapabilities caps = new DesiredCapabilities();
      //FirefoxProfile firefoxProfile = new ProfilesIni().getProfile("default");
     // WebDriver driver = new FirefoxDriver(firefoxProfile);
      //DesiredCapabilities dc = DesiredCapabilities.firefox();
      //driver = new InternetExplorerDriver();
      driver = new FirefoxDriver();
      baseUrl = "http://localhost:8080/";
      driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
      driver.manage().window().maximize();
      driver.manage().deleteAllCookies();
  }

  @Test
  public void testUntitled() throws Exception {

    driver.get(baseUrl+"/Nuemera/promotional");
    assertEquals("BLOCK MY PHONE - Registration Benefits",driver.getTitle());

                    /*Testing Benefits------starts*/
    driver.findElement(By.linkText("Benefits")).click();
    str = driver.findElement(By.xpath("//div[@class = 'benefits text-center no-mobile']/h1")).getText();
    System.out.println(str);
    assertEquals(str,"STOLEN? BLOCK IT NOW!");
                    /*Testing Benefits------ends*/

                    /*Testing FAQ------starts*/
   driver.findElement(By.linkText("FAQ")).click();
   str =  driver.findElement(By.xpath("//div[@class = 'box faqheader']/h2")).getText();
   System.out.println(str);
   assertEquals(str,"Frequently Asked Questions");
  // Thread.sleep(2000);
                    /*Testing FAQ------ends*/
   File snapshot =((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);           
   System.out.println(snapshot);

                    /*Testing Contact-------starts*/
    driver.findElement(By.linkText("Contact")).click();
    str = driver.findElement(By.xpath("//div[@class = 'box grey']/div/p")).getText();
    System.out.println(str);
    assertEquals(str,"Coming soon...");
    //Thread.sleep(2000);
                    /*Testing Contact ------ends*/

                    /*Testing Login button--------starts*/
    driver.findElement(By.linkText("Login")).click();
    str = driver.findElement(By.xpath("//div[@id = 'loginDiv1']/form/a")).getText();
    System.out.println(str);
    assertEquals(str,"Sign In");
    //Thread.sleep(2000);
                    /*Testing Login button--------ends*/

      /*Testing successfully Login or Not------starts*/
    driver.findElement(By.id("userId")).clear();
    driver.findElement(By.id("userId")).sendKeys("[email protected]");
    driver.findElement(By.id("pwd")).clear();
    driver.findElement(By.id("pwd")).sendKeys("xxx");
    driver.findElement(By.xpath("//div[@id = 'loginDiv1']/form/a")).click();
    str = driver.findElement(By.xpath("//div[@class = 'container']/nav/ul/li[6]/a")).getText();
    System.out.println(str);
    assertEquals(str,"[email protected]   Log Out");
    /*Testing successfully Login or Not------ends*/

    driver.findElement(By.xpath("//div[@id = 'deviceList']/div/div/div/div/a")).click();
    driver.findElement(By.xpath("//div[@class = 'fancybox-skin']/a")).click();
    Thread.sleep(2000);         
    System.out.println("done");

  }
@After
  public void tearDown() throws Exception {
    driver.quit();
    String verificationErrorString = verificationErrors.toString();
    if (!"".equals(verificationErrorString)) {
      fail(verificationErrorString);
    }
  }

/*@AfterClass
public void testAfterClass() throws Exception{

}*/

  private boolean isElementPresent(By by) {
    try {
      driver.findElement(by);
      return true;
    } catch (NoSuchElementException e) {
      return false;
    }
  }

  private boolean isAlertPresent() {
    try {
      driver.switchTo().alert();
      return true;
    } catch (NoAlertPresentException e) {
      return false;
    }
  }

  private String closeAlertAndGetItsText() {
    try {
      Alert alert = driver.switchTo().alert();
      String alertText = alert.getText();
      if (acceptNextAlert) {
        alert.accept();
      } else {
        alert.dismiss();
      }
      return alertText;
    } finally {
      acceptNextAlert = true;
    }
  }
}

2 Answers 2

2

@siva prasad: Did you use wait in your script?, if not please use below code may solve your problem. If not please paste your code so we can look into it.

driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
1
  • i already added the above line but it's not working, i just updated my code above please check and guide me thank you Apr 22, 2014 at 4:25
1

I suggest to run Fiddler or some other HTTP debugging proxy on the machine where your test is executed to check what is actually sent as part of a request. It may be that you use some outdated cookie value that results in "Session timeout". If it is indeed a problem with cookie, you can delete all cookies when you instantiate your webdriver:webdriver.manage().deleteAllCookies() (see api docs).

Besides, you can try to use different browser profile when instantiating your webdriver (for example, look here for info on Firefox profiles). By default, a new anonymous profile is created, but you can also create a dedicated profile for your test execution (look here for more info).

4
  • Hi oldbam, i just updated my code see above, still not working even i used that deleteCookies() line........Thank you Apr 22, 2014 at 4:27
  • did you check what is being sent as part of the request to the server? can you simulate the same error manually? if you can't reproduce it manually when you open the browser and do the same actions that your test does, you may try to run your browser in the automated test with the same profile that you use during manual testing, most probably the name of the browser profile that you use will be "default", but you can also verify it by running firefox.exe -p. search the web to find how to run firefox with existing profile via selenium
    – oldbam
    Apr 22, 2014 at 9:45
  • @oldbam........if i did manually it is fine.... Apr 22, 2014 at 11:28
  • try to run your test with default firefox profile. if it is named "default", this is how you obtain it: ProfilesIni profile = new ProfilesIni(); FirefoxProfile ffprofile = profile.getProfile("default"); WebDriver driver = new FirefoxDriver(ffprofile);
    – oldbam
    Apr 22, 2014 at 12:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.