-1

Which values do I have to put in actual and expected if that result is greater than 5000 and what can I do to print pass in the console?

Code trials:

public class Grabthe_result_google {

    public static void main(String[] args) {
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--remote-allow-origins=*");
        
        System.setProperty("webdriver.chrome.driver", "C:\\Users\\l\\OneDrive\\Desktop\\chromedriver-win32\\chromedriver.exe");
        WebDriver driver=new ChromeDriver(options);
        driver.get("https://www.google.com");
        driver.findElement(By.id("APjFqb")).sendKeys("mobile",Keys.ENTER);
        String result = driver.findElement(By.id("result-stats")).getText().split(" ")[1];
     System.out.println(result);
    Assert.assertEquals(0, 0);
    }
}
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

2 Answers2

0

If I understood you correct, is this case you can use assertTrue.

Here is example of code:

driver.findElement(By.id("APjFqb")).sendKeys("mobile",Keys.ENTER);
String result = driver.findElement(By.id("result-stats")).getText().split(" ")[1].replaceAll("," ,"");
Assert.assertTrue(Long.parseLong(result) > 5000);
Yaroslavm
  • 1,762
  • 2
  • 7
  • 15
0

To assert if the number of search result value is more then 5000, you have to:

  • Induce WebDriverWait for the visibilityOfElementLocated() and extract the text.

  • Split the text with respect to the blank character and consider the second part.

  • Remove the , characters.

  • Use assertTrue() from HardAssert after converting the string to long using parseLong()

  • You can use either of the following locator strategies:

    driver.get("https://www.google.com");
    new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.name("q"))).sendKeys("mobile" +Keys.chord(Keys.RETURN));
    String result = new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div#result-stats"))).getText().split(" ")[1];
    System.out.println(result);
    String myresult = result.replace(",","");
    System.out.println(myresult);
    Assert.assertTrue(Long.parseLong(myresult) > 5000);
    
  • Console output:

    21,22,00,00,000
    21220000000
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352