0

I am writing selenium web driver automation test cases. sometimes i see this weird behavior in selenium. I run my tests(TESTNG) and it run successfully and i again run the same code nothing happens.

Again the same problem came to me with the following code

I am just writing one method here

 @BeforeTest
public void method(){
driver.get("http://site.staging.snapdeal.com:7003/product/intex-aqua-n2-white/737345766");
driver.manage().window().maximize();
window = driver.getWindowHandle();
driver.switchTo().frame("loginIframe");
driver.findElement(By.id("close-pop")).click();
driver.switchTo().window(window);
wait.until(ExpectedConditions.presenceOfElementLocated(By.id("BuyButton-2")));
productName=driver.findElement(By.xpath(".//div[@class='productTitle']//h1")).getText();
System.out.println("Product Name : "+productName);  
driver.findElement(By.id("BuyButton-2")).click();
wait.until(ExpectedConditions.presenceOfElementLocated(By.className("cart-scroll")));
System.out.println("button clicked");
}

once this code will run fine and just on another run my program gets lost in some other world neither does it throws any exception nor times out.. just the browser comes to an idle state after the 4th last line driver.findElement(By.id("BuyButton-2")).click();

this button gets clicked the required action (opening of cart) occurs and then the browser is still. i do not see the "button clicked" output on my console

I do have the driver.quit(); call in my @Aftertest method.

but neither the driver quits nor does it throws any exception and nothing on the console either.

is something wrong with my code?

Alpana Chauhan
  • 407
  • 1
  • 9
  • 18
  • Probably class "cart-scroll" is not loaded after clicking the "BuyButton-2". Maybe you are missing some steps before clicking the button. Did you check the presence of "cart-scroll" class manually? – ntsh Sep 05 '14 at 12:33

2 Answers2

0

To me, it looks like it could be related to the .switchTo().frame call. Apparently, you have more than one window open at once. If your focus is not on the correct frame in the correct window, then you would get the sort of behavior that you are describing. One thing I do to make sure I have the right driver context is to do a JavascriptExecutor call that brings the correct window to focus (but obviously that wont help with the frame).

You will need to sort all this out because there is not enough information here. If you could put together a completely working GitHub example that I could load and troubleshoot, then we could help more.

I would recommend writing example code against the GitHub project called "The-Internet", then it is a shared context by which we can better answer you.

djangofan
  • 28,471
  • 61
  • 196
  • 289
0

I faced similar issue in my framework as well but while investigating the root cause I found that the culprit was a java script based wait method. So I replaced that method and it worked for me. But your code does not have such kind java script based wait method.

I feel there is something wrong with selenium wait conditions and in your case execution gets stuck at wait.until(ExpectedConditions.presenceOfElementLocated(By.className("cart-scroll")));

It means it keep waiting for expected and never time out without any exception.

Have a look here: https://code.google.com/p/selenium/issues/detail?id=6955

Selenium webdriver execution gets stuck due to a javascript wait condition

Community
  • 1
  • 1
Priyanshu
  • 3,040
  • 3
  • 27
  • 34