0

Pretty new to Selenium. I have this javascript code snippet and I'm trying to use Selenium to inspect the script with no luck. All I want to do is use Selenium to access/inspect the script to verify that it is on my page.

Anonymous function:

<script type="text/javascript"> 
    (function(a,b,c,d){
        a='https://someurl.com:24800/somejs.js';
        b=document;
        c='script';
        d=b.createElement(c);d.src=a;d.type='text/java'+c;
        d.async=true;
        a=b.getElementsByTagName(c)[0];
        if (a.src !== d.src) { 
            a.parentNode.insertBefore(d,a);
        }
    })();
</script>

To make the problem more clear, in another scenario I have successfully been able to retrieve a snippet of javascript that is assigned to a variable name like this:

<script type="text/javascript"> 
    var my_var = { attribute_1:'something', attribute_2:'somethingElse'} 
</script>

Then in my Selenium test...

JavascriptExecutor jsEx = (JavascriptExecutor) driver;
Map<String, String> topScript = (Map<String, String>)jsEx.executeScript("return my_var");

This works fine and my topScript returns the script that I am able to parse. I want to do the same thing with the anonymous function above (if possible)

I've already tried these posts, no dice:

Selenium calling anonymous function generates syntax error

Selenium and asynchronos JavaScript calls

Community
  • 1
  • 1
Bradley D
  • 2,333
  • 1
  • 15
  • 19
  • Don't anonymous functions execute themselves? It seems really hard to reference something that has no name – bichito Feb 04 '17 at 00:07
  • I do not know, nor care if the script executes itself. I've modified my question title to make it less confusing. I am simply trying to verify that this script exists on my page. That is my objective. – Bradley D Feb 04 '17 at 00:13
  • I suppose that if it cannot be referenced, that answers my question. But if I could reference the attributes inside of it, that would work for what I'm going for as well. – Bradley D Feb 04 '17 at 00:15
  • Try any of the selenium search functions and retrieve the contents. verifyTextPresent is a brute force approach. If this does the trick let me know and I will post it as answer – bichito Feb 04 '17 at 00:21

1 Answers1

1

The SCRIPT tag is an HTML tag like any other. You can locate it and text contained using an XPath.

List<WebElement> scripts = driver.findElements(By.xpath("//script[contains(., 'function(a,b,c,d)')]"));
if (!scripts.isEmpty())
{
    // the script tag with the anonymous function was found
    // do stuff
}

I like to write functions for code like this that I plan to reuse. The function below finds the code you pass in and returns true if found.

public static boolean findCodeInScriptTag(String code)
{
    return !driver.findElements(By.xpath("//script[contains(., '" + code + "')]")).isEmpty();
}

NOTE: You will want to pass in enough code to make sure you are locating the script tag you intend to, e.g. if you aren't specific enough you may be locating more than one instance of the code and return false pass results.

JeffC
  • 22,180
  • 5
  • 32
  • 55