Pergunta

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

Foi útil?

Solução

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top