0

Could someone please assist me in solving following:

I have to test does source code of page contains following line:

<script>dataLayer.push({'login':'not logged in','site-language':'es','article-type':'2','section1':'error page','section2':'error page > access denied'});</script>

Actually I am looking for access denied or error page > access denied if exist on that page and get stuck when found page source

 driver.navigate().to(BaseTest.config("url") + "/es/pronosticos");
   String link = driver.getPageSource();

Which has to be next step to check if above line exist on current page? Thank you in advance

1 Answer 1

1

What you are trying to do is basic String manipulation.

You can perform operations like matches() or contains() methods on String.

Example:

driver.navigate().to(BaseTest.config("url") + "/es/pronosticos");
String link = driver.getPageSource();
String script = "<script>dataLayer.push({'login':'not logged in','site-language':'es','article-type':'2','section1':'error page','section2':'error page > access denied'});</script>";

if (link.contains(script)) System.out.println("Site contains script");
else System.out.println("Site does NOT contain script");

The other approach would be trying to find <script> element via WebDriver

try {
    WebElement script = driver.findElement(By.tagName("script"));
    String scriptText = script.getText(); //returns the text of <script> tag
    if (scriptText.contains("access denied")) System.out.println("Access Denied!");
    else System.out.println("Access granted");
} catch (NoSuchElementException e) {
    System.out.println("Script does NOT exist!");
}
Sign up to request clarification or add additional context in comments.

1 Comment

Laskowski Thanks, second approach is perfect!

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.