Using Selenium to Test jQuery element data()? - javascript

Is it possible to use selenium to test whether a jQuery data() is set on an element?
In my app, the user enters information about several orders. Each order gets appended to an overview table so they can see their progress as they're going. In addition to the visible order information, we store some additional information that we need about each order using an array stored in jquery.data(), like this:
$('#table').data('orders').push(neworderdata)
I'd like to write a selenium test to assert that when the user presses 'Add order', my additional data gets added to $('#table').data('orders').
I think I need something like:
Command: assertEval
Target: $('#table').data('order')[1].cust_no
Value: 99999
But I think the issue I'm having is that Selenium IDE doesn't know about the jQuery namespace, and functions, so it doesn't know how to find the table. But even if I used getElementbyID('table'), how do I get selenium to know about the data()?

Are you sure that Selenium IDE is having a problem with $? I was able to access it from Selenium IDE. Perhaps something else is broken in you jQuery expression? Can you first try "assertEval $ 0" etc. to see if the IDE can evaluate $ alone?

I find that selenium will only see the jquery function if you access it as:
window.$(...)
not as
$(...)
Perhaps that's (part of) the problem?

Related

New Selenium IDE, how to write a conditional statement with stored attribute

I was able to store the alt value attribute. now i want to write a condition statement that says,
if the Stored attribute is "iwantthistextvalue" then true.
this is what i am trying that is not working
${x} === "iwantthistextvalue"
Use approach as i mentioned in screenshot below.
Replace command where I store variable with 'store attribute'
Here is Selenium IDE code: if...else condition code
Side by side, you can see the original documentation of control flow from Selenium IDE. That section describes all possible control flow & conditional commands. Check this link, it is really very helpful:
https://www.selenium.dev/selenium-ide/docs/en/introduction/control-flow

How can i send character sequence to a contenteditable="true" element by using selenium and python

lately, I try to use python3.6 and selenium to do a small task, I want to log in a chatting webpage and send the message automatically.
http://maimai.cn/?regfr=baidu_pc_titlethis is the webpage that I want to send the message to
In my limited experience with python, I only know that the input element can send string. However, I came across this tough question.
I google a lot and I know that I need to use JavaScript in my python code to insert the string into a div element whose attribute contenteditable is true.
when I run my code like this:
my_desired_text="lzylzylzy"
driver.execute_script("document.getElementsByClassName('inputPanel'[0].innerHTML="+ my_desired_text)
it warns me like that:
selenium.common.exceptions.WebDriverException: Message: unknown error: lzylzylzy is not defined
but I indeed define my string.
I am searching for a long time on the net. But no use, please help or try to give some ideas on how to achieve this. Thanks in advance.
In my project, there is an iframe element to be located firstly.I locate an element in the iframe but I forget to switch to the main frame.This element is in the main frame.That is why i can't locate it.If you meet the similar situation, you can check whether you switch to your main frame and use .sendkey() method to send string.

How do I use WebdriverIO (wdio) to get the number of times an element appears?

I'm trying to make an interaction test for my javascript code using WebdriverIO (wdio).
I want to test a delete node functionality by checking the number of times a selector appears before and after my delete function and seeing that it was decremented by one.
How do you use wdio to find the number of times a selector appears on a page?
There is an elements command called in the api for the browser object.
you can use it thus:
browser.elements(selector).value.length
Note that elements returns an object. You need to go into value to get the array.
I've also read here that you can also use the shortcut to browser elements $$ as so:
$$(selector).length. That didn't work for me but it might be because I'm running an older version of wdio or some random configuration issue.

jQuery variable name appended with a large number

I often see in my code I am loading from my company's media central that jQuery is not available in the console normally. (No $ and jQuery)
But sometimes, to those elements to which jQuery is attached, it has a long number with it.
jQuery18306575689211022109_1378907534666
What is the purpose of doing this? Security?
Also, jQuery is sometimes available directly in the console, but only with the above numbers.
I am therefore unable to debug my apps in console, where I need to query using jQuery.
However, in the JavaScript code, jQuery is perfectly being used as $ and jQuery. So I apply a break-point and export as window.jQuery = jQuery.
Is this a proper way to debug the app, when jQuery is obfuscated?
UPDATE:
For eg., check this URL the app is calling. It seems that the URL knows what is the number appended to jQuery. How do I come to know of the same number while debugging? Any lights on what is going on?
It's a callback id automatically generated by jQuery. See the documentation for jsonpCallback.
It is preferable to let jQuery generate a unique name as it'll make it easier to manage the requests and provide callbacks and error handling.
It seems that you are confusing two different variables. You can make a quick test on this website : http://www.jquery4u.com/function-demos/jsonp/#demo. Click "run demo", open Chrome's console, type "jQuery" in order to retrieve the callback's name, then perform this simple comparison :
jQuery16409391013463027775_1379048051365 === jQuery // false
That said, the fact that the variables named "$" and "jQuery" are not available in your case may be due to a specific implementation. One possibility could be the use of jQuery.noConflict() which allows either to customize the name used as a reference to jQuery, or to completely remove all references to jQuery from the global scope (window), so there is no way to access it in the console.

Calling a function in a JavaScript file with Selenium IDE

So, I'm running these Selenium IDE tests against a site I'm working on. Everything about the tests themselves is running fine, except I would like to do a bit of clean-up once I'm done. In my MVC3 Razor based site, I have a JavaScript file with a function that gets a JsonResult from a Controller of mine. That Controller handles the database clean-up that Selenium IDE otherwise couldn't handle.
However, I'm having a hard time finding any sort of documentation on how to do this. I know I can do JavaScript{ myJavascriptGoesHere } as one of the Values for a line in the test, but I can't seem to find a way to tell it to go find my clean-up function.
Is it even possible for Selenium IDE to do this sort of thing?
If it comes down to it, I can just make a separate View to handle the clean-up, but I'd really like to avoid that if possible.
Thanks!
If you want to execute your own JavaScript function that exists in your test page from Selenium IDE, you need to make sure you access it via the window object. If you look at the reference for storeEval for instance, it says:
Note that, by default, the snippet will run 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 if you have your own function e.g. myFunc(). You need to refer to it as window.myFunc().
This can be very handy for exercising client-side validation without actually submitting the form, e.g. if you want to test a variety of invalid and valid form field values.
If you use runScript, that should already run in the window's context.
This works for me.
IJavaScriptExecutor js = driver as IJavaScriptExecutor;
string title = (string)js.ExecuteScript("myJavascriptGoesHere");
Make sure your javascript works first before using it here!
Actually to access your page javascript space, you need to get the real window of your page : this.browserbot.getUserWindow()
See this statement to get the jQuery entry point in your page (if it has jQuery of course ^^ )
https://stackoverflow.com/a/54887281/2143734

Categories

Resources