0

Got the following code:

 driver.findElement(By.id("input_search")).click();
 driver.findElement(By.id("input_search")).clear();
 if(lower3 == true){
    //read a line from a doc 
    document_path = "C:\\MyProject\\src\\harness\\lower_than_3" 
    FileInputStream fis = new FileInputStream(document_path);  
    BufferedReader br = new BufferedReader(new InputStreamReader(fis));         
    while((line = br.readLine()) != null){
        //line is inserted into search field
        driver.findElement(By.id("input_search")).sendKeys(line);
        Thread.sleep(100);     

       //press search button- page should NOT refresh if string length for search is lower than three, NO search is performed
       driver.findElement(By.cssSelector("div.btn-img.submit")).click();

       //ASSERT OR VERIFY IF PAGE IS RELOADED OR NOT AFTER CLICK ON SEARCH BUTTON
       NEED HELP         
    }
    br.close();   
 }

After search click on search buton is executed (line 15 from code region) I want to check if page refresh event occurs. I DON'T WANT TO REFRESH PAGE (there are so mane examples with that :) ) I JUST WANT TO CHECK IF PAGE IS REFRESHED OR NOT

Cœur
  • 37,241
  • 25
  • 195
  • 267
vasilev21
  • 113
  • 2
  • 4
  • 15
  • 1
    It is better if you could check the element after search action performed instead of checking page is refreshed or not. – Abhishek Yadav May 01 '14 at 12:30
  • Agree with @AbhishekYadav, you'd be better off checking a condition on the page, is there any validation messages presented when the search string is < 3 characters? If so, you should assert that that message is present – Steve Weaver Crawford May 01 '14 at 16:48
  • @Steve Crawford and Abhishek Yadav - there are no messages and no changes on the page also no refresh event if search string is < 3 characters. I just want to try this for learning purposes. I did not found any solutions to this on internet. I know a few methods to refresh the page but I did not identified any method to check this event. In your opinion (both of you) if I would check for an interval of 10 seconds if an element (present only on root page of the site) can take click action or is present on page could be a valid condition to check that no refresh event took place. – vasilev21 May 01 '14 at 20:12
  • I guess that would work, but it's not ideal. Before searching do a findElement to get an element on the page. After searching if the page has refreshed you will get a StaleElementException if you try to click on it. If the page has not refreshed the click will be successful – Steve Weaver Crawford May 01 '14 at 20:21

1 Answers1

3

You can use any of these to solve your purpose:

Solution 1: Verify an element after search action performed.

driver.findElement(By.id("input_search")).clear();
driver.findElement(By.id("input_search")).click();

//here write code to wait for an element available on search result page

try{
if(driver.findElement(By.id("search result page element")).isDisplayed() == true){
System.out.println("Search successful");
} catch (NoSuchElementFound ee){System.out.println("Search not successful");}

Solution 2: Check for page loaded or not after search action performed using java script.

driver.findElement(By.id("input_search")).clear();
driver.findElement(By.id("input_search")).click();

//here write code to wait for sometime around 10-20 seconds

JavascriptExecutor jsExecutor = (JavascriptExecutor)driver;
String s = (String) jsExecutor.executeScript("var s1 = 'Search successful'; var s2 = 'Search not successful'; if(document.readyState === 'complete'){return s1;} else {return s2;}");

Both the code snippet is tested for Google.

Abhishek Yadav
  • 459
  • 5
  • 16