Want to asser text present in Page Source Code or not
//Only way you can read a variable is using javascript//with <code>executeScript</code> method as shown bellow.private void test(){ String script = "return rlSerial;"; String value = (String) ((JavascriptExecutor)driver).executeScript(script); System.out.println(value); //Use assert here}
You can just loop through all the SCRIPT
tags and search for the desired text.
List<WebElement> scriptTags = driver.findElements(By.tagName("script"));for (WebElement scriptTag : scriptTags){ if (scriptTag.getAttribute("innerHTML").contains("var rlSerial = '1058358';")) { // assertTrue() break; }}
EDIT:I just ran the code below and it worked fine for me... must be something on your end.
driver.get("http://www.google.com");String searchText = "document.body.style.visibility";List<WebElement> scriptTags = driver.findElements(By.tagName("script"));for (WebElement scriptTag : scriptTags){ String text = scriptTag.getAttribute("outerHTML"); if (text.contains(searchText)) { System.out.println("Found it: " + text); // your assertTrue(...) goes here break; }}
Console:
Found it: <script>if(google.j.b)document.body.style.visibility='hidden';</script>