Tuesday, July 1, 2014

Coded UI JQuery Selectors


Automate Your CodedUI Scripts with J Query Selectors


Make your scripts work with JQuery selectors, for web automation testing. As we know that codedui does not support either css selectors/xpath to identify elements, it would be little hard for the people who are habituated with Automation Tools which support Css selectors and Xpath mechanism. Here is a solution that we can use with CodedUI to work with JQuery selectors inplace of Css and XPath mechanism.
                                       All you need to do is to invoke the javascript on your webBrowser that uses jquery selector mechanism which is faster than regular xpath. Ofcourse, it take time in identifying the control than Regular CodedUI Identification Mechanism, but is just an alternative that you can use for Other Browsers where the execution time takes a lot.

         
          Follow the Steps: 
         This statements ensures that your webpage has jQuery capability for run your jquery selectors 
          on it.        
     bool createJSTag = (bool)Application.browser.ExecuteScript("return (typeof $ === 'undefined')");
     
     If the above statement returns false, then we should create a script tag that refers to
     jquery  '.js file'. This can be done using the below code.

                if (!createJSTag)
                {
                    Application.browser.ExecuteScript(@"
                    var scheme =  window.location.protocol;
                    if(scheme != 'https:')
                        scheme = 'http:';

                    var script = document.createElement('script');
                    script.type = 'text/javascript';
                    script.src = scheme + '//code.jquery.com/jquery-1.10.1.min.js';
                    document.getElementsByTagName('head')[0].appendChild(script);
                ");
                }
     Once, you create a jquery script tag into your webpage dynamically, call your JQuery
     Selectors, on the webpage.

     Example Selector: $(\"div[class='two columns'] input:first-child\")

     HtmlControl control= ((HtmlControl)Application.browser.ExecuteScript("return " + jquerySelector + "[0];"));

No comments:

Post a Comment