How to execute JavaScript in Robot Framework - selenium

Below is my code. When I run this, it shows a WebDriverException. How do I execute JavaScript code in Robot Framework?
This, return $(arguments[0]).data('${ToolTip}').options.title code is doing well in -java Selenium web driver.
Mouse Over ${CreateTask}
Execute JavaScript return $(arguments[0]).data('${ToolTip}').options.title

From http://rtomac.github.io/robotframework-selenium2library/doc/Selenium2Library.html#Execute%20Javascript:
Note that, by default, the code will be executed in the context of the
Selenium object itself, so this will refer to the Selenium object. Use
window to refer to the window of your application, e.g.
window.document.getElementById('foo').
So
Mouse Over ${CreateTask}
Execute JavaScript return window.$(arguments[0]).data('${ToolTip}').options.title
Assuming there is some library (jQuery most probably) that actually understands the $ shorthand.

arguments[0] normally refers to arguments passed to your script.
Selenium2Library's Execute JavaScript calls
webdriver.execute_javascript and does not pass any arguments to it. arguments[0] is therefore undefined.
See Is there a way to provide arguments to "Execute JavaScript" in Robot Framework? for a workaround.

Use any one method from below options
Execute Javascript document.getElementById('authUser').value='admin'
${ele} Get WebElement id=clearPass
Execute Javascript arguments[0].value='pass'; ARGUMENTS ${ele}
Execute Javascript alert(arguments[0]); ARGUMENTS 123
Execute Javascript document.evaluate("//*[text()='Patients']",document.body,null,9,null).singleNodeValue.click();
Execute Javascript document.querySelector('[data-bind="click: changePassword"]').click();
${ele} Get WebElement //*[text()='Logout']
Execute Javascript arguments[0].click(); ARGUMENTS ${ele}

Related

In TestCafe Studio, how can I import/reference selectors/functions/custom scripts defined in one fixture into another?

If I define a selector or function in one test, can I access it in another test or a test in another file/fixture? Similarly, is there a way to implement page models and for tests to use selectors defined within page?
TestCafe Studio does not allow sharing selectors and functions defined via the "Define Element Selector" and "Define Function" actions in codeless testing. However, you can import selectors using the Run TestCafe Script action.
For example:

Capybara: page.execute_script() not working

I am trying to simulate drag and drop using jquery.simulate library in rspec feature.The execute_script lines in spec are:
page.execute_script("$('#slide_1').draggable();")
page.evaluate_script("$('#slide_1').simulate('drag', {dragTarget: '#library_swap', interpolation: {stepWidth: 10, stepDelay: 300 }});")
page.evaluate_script("$('#slide_1').simulate('drop');")
If I run the lines inside the execute script over chrome console,its working fine(drag-drop simulation works),but not working with execute_script
Since you're not getting any errors, the JS code you're passing to execute_script is actually being executed. Since you're not seeing the behavior you expect the most likely explanation is that you're executing the JS before the element is actually on the page, which would then just silently do nothing. The one thing that confuses me about the code is why you're calling draggable on the #slide_1 element since I would assume that had already been called in your app. Anyway - add an expectation before your execute_script calls to make sure the element is actually on the page
expect(page).to have_css('#slide_1')
execute_script("$('#slide_1')...
Also note, there shouldn't be any reason you need to use three different execute_script calls for this, you could just combine them all into one. In recent versions of Capybara you can also DRY up the commands by not specifying the selector again and instead passing the element to execute_script
slider = page.find(:css, '#slide_1')
execute_script("$(arguments[0]). ... ", slider)

Selenium Out of focus element

I have a scenario in selenium where I have a web element which is available on the page but to reach out to that web element sometimes we have to scroll down depending upon some business logic.
I don't want to use javascript executor and css selector. Is there any other way we can check if element is not clickable I can try to see if there is a scroll down scroller available? And my driver should scroll down and try to check that element once before it actually throw exception.
Let me try to answer your Questions one by one.
we have to scroll down : When you think of scroll its worth mentioning that scroll method is not a method of Webdriver, this is a method of JavaScript. JavascriptExecutor is an Interface which is available in package org.openqa.selenium.JavascriptExecutor. Inside this Interface we have a method called executeScript() – so whatever script you will pass as a string it will be executed by JavascriptExecutor.
I don't want to use javascript executor : We are not using anything separate stuff by the name JavascriptExecutor. We are using the same instance of WebDriver i.e. driver, but we need to downcast it so that it can act as a JavascriptExecutor to perform whatever script you will pass as a string.
css selector : Using css selector is not mandatory. Selenium provides you the flexibility to use any of the properties of a particular element available like id/name/linkText/xpath.
Let me know if this answers your Question.

By is not a function in selenium nightwatch

I could not use the default By commands in selenium. Below is my script
driver.findElement(By.xpath("//*[#id='lotstatus_q']/div/table/tbody/tr"));
my script is failing and the result is this:By is not a function
is there any other items that I need to install?
All other By commands are not functioning.
I think you made a mistake, you cannot directly access to Selenium API from Nightwatch.js !
Nightwatch is just an override of the WebDriver Wire Protocole (exposed by Selenium as REST API)
https://code.google.com/p/selenium/wiki/JsonWireProtocol
But you can use XPath selectors with Nightwatch:
client.useXpath().waitForElementVisible("//*[#id='lotstatus_q']/div/table/tbody/tr")
Read the doc about XPath: http://nightwatchjs.org/guide#using-xpath-selectors
You cannot apply the regular Java patterns with NightWatchJS and also NightWatch Apis does not work that way...
Please see the API docs on how you can use http://nightwatchjs.org/api#commands
I think you are trying to do
browser.elements("css locator" , "value of your css", function (result){
// here result is the return from calling selenium wire protocol /elements
// Using the identifier of your CSS locators
})

Selenium: Ajax Testing

Please brief me about the Ajax testing with selenium RC.
As in Ajax element not reflect on the view-source, but using firebug we can see the changes in HTML source code.
There are two methods associated with it Ajax testing..
1-The method "waitForCondition (java.lang.String script, java.lang.String timeout), to provide script we have to create a java script by own or it should be the same Ajax script/java script present on the web page.
Please correct me if i am wrong on below point..
2-The method "waitForElemantPresent(Locator)", We check the Element in the firebug and check the same in this method is self waitForElemantPresent(Locator).
Let me know if anything else I am missing testing Ajax application.
I got help on this from one article and with help of #Hannibal
http://agilesoftwaretesting.com/?p=111
JQuery: “jQuery.active”
Prototype: “Ajax.activeRequestCount”
Dojo: “dojo.io.XMLHTTPTransport.inFlight.length”
So if there is Ajax call we can use second option.
selenium.waitForCondition(
"selenium.browserbot.getCurrentWindow().jQuery.active == 0",
timeout);
To answer your first point, yes waitForCondition(javascript,timeout) will run the javascript till it returns a true value OR when timeout happens. You should take a look at the api documentation for this as you need to use browserbot to run the script in your application window. Link to API documentation is here
In Selenium 1, one way by which you can handle the Ajax conditions are by creating custom functions which will wait till the condition is met or till a timeout happens. While a normal Selenium.isElementPresent will fail immediately if the element is not present, your custom function will wait for some more time (the time for Ajax to load) before it fails. As an example you can refer the following custom method for isElementPresent. This is written in JAVA, you should be able to use the same logic in the programming language that you use.
public boolean AjaxIsElementPresent(String elementToLookFor, int timeOutValueInSeconds){
int timer=1;
while(timer<=timeOutValue){
if(selenium.isElementPresent(locator))
return true;
else
Thread.sleep(1000);
}
return false;
}
This method will return a boolean false if the element is not present even after the specified timeoutValue. If it finds the element within the timeoutvalue then it returns a true.
I have seen some in built functions for AjaxCondition handling in Selenium 2. But I have not used it. You can refer to Selenium 2 code base