How to set value of a hidden input with Selenium? - ruby

I've already looked at this but had no luck.
I've tried that example and it says undefined browserbot, I also tried the simple:
#browser.navigate.to "http://example.com"
#browser.execute_script("$('#hiddenthing').val('foo bar')")
which doesn't work at all, If i tried to set the value without javascript, it says you can't interact with hidden elements.
Any suggestions?

Selenium WebDriver cannot interact with hidden elements, it can only find them. If you attempt to do any user based interaction on a hidden element, you will get the error you saw above.
This is because SWD was created to emulate the things a user can directly do (with a few exceptions). Being able to interact with hidden elements falls outside the scope of SWD.
However, SWD does provide the ability to inject any javascript into the DOM of the browser (which makes handling these types of requirements more reasonable, if just a bit more difficult).
Try these two ways by executing javascript (as you saw from the above thread you linked to). Just remember that it requires the use of the return command:
#browser.execute_script("return document.getElementById('hiddinthing').value = 'foo';")
or if you do have jQuery
#browser.execute_script("return $('#hiddenthing').val('foo');")

Related

Race condition with Capybara value set

I have faced an issue while using .set(#{value}) to fill the text field in registering form, e.g: the phone number i wanna put in is 506307 then it ended up with 063075.
The work-around i have been made is executing Javascript block like
execute_script("document.querySelector('#{selector}').value = '#{value}'")
However, using the same scripts applying for Webmobile based on React.JS, the scripts above just send the text but didn't send the onChange event, which cause another element cannot be selected/clicked -> made the test failed.
I came up with another approach is to use the send_keys #{value} to trigger the key-pressed event that would make browser think there was a key-pressed event happen for that form, but it ended up with race-condition like set(#{value}) as i mentioned.
The another work-around is using What is the best way to trigger onchange event in react js , but i tend to use the native Capybara actions before making that tricky Javascript.
So, is there any other way to interact / fill the form field which won't cause that Race condition issue ?
Thanks everybody in advance.
Note: Any "solution" suggested that is purely the use of execute_script to run some JS is a terrible idea since it completely bypasses the concept of testing what a user can do and can basically make your test worthless.
The root cause of the issue here is the JS behavior attached to the input not being able to handle the key events fast enough. The proper fix would be to fix the JS, however if that's not possible there's a few things you can try
First you can try changing the clear method being used by set
element.set('506307', clear: :backspace)
or
element.set('506307', clear: :none)
If that doesn't change anything then try clicking on the input, followed by a short sleep before setting the content
element.click
sleep 0.25
element.set('506307')
If none of those work around the issue we need to know exactly what JS behavior you have attached to the input and/or what events that JS behavior is listening to.

How do I access this variable from a different blueprint?

I have found loads of stuff like this on the Internet, but none of it is helping me and I would just love a direct response for this situation.
I have a text UI for my ammo which I simply want to access from the blueprint that handles firing. The text UI has a variable associated with it (At least, I assume this is my text UI):
But for unknown reasons won't let me make it public. That doesn't seem to matter though, because I have experimented with other public variables and I can't access them from my blueprint either!
How can I get access to my variable here so I can do what I wish?
I found your other questions so I assume you're using 4.12, like myself.
So I can use the variable declared in UMG as follows:
Step 1, make a text block, enable it as Is Variable:
Step 2, check it from the Graph part:
Step 3, some other Blueprint, use it:

Buildup and reference of objects in HP UFT

I'm wondering how I can access properties/methods via console/watch.
I have the following code:
Dim page
page = Browser("Welcome: Mercury Tours").Page
Now I want to obtain the title of this Page. Since I inspected the Page object with Object Spy and I saw it has a title property.
When I enter page.title in my watch however, it tells me that page does not contain the property.
1. What is the correct syntax?
2. Why is this not working? I presume that the watch is checking for VBScript object properties instead of TestObject properties?
(I have a programming background and I find it very confusing that I have VBObjects and TestObjects simply walking through the same file. It kind of feels like a black box :/)
Ok, well, your syntax is incorrect...
It appears that you're trying to put something into a variable called "page", but I'm not sure if I can figure out your intention.
If you are trying to put the page object into the var "page", you would need to use a set statement (to indicate to vbscript that it's going to hold an object, not just a single piece of data)...
Regardless of that, your syntax for specifying the Page is wrong.
In your example, you're specifying a browser test object called "Welcome: Mercury Tours" from the repository... but then you put .Page - and that's where your syntax error is.
It helps to understand the difference between Test Objects and Realtime Objects - because you need to specify a page Test Object. You can do that by specifying a page object from the Object Repository, or you can do it descriptively.
Test Objects are descriptions of real objects that QTP tries to find. If it successfully finds a real object that matches the description, then the Test Object kind of (virtually) "attaches to" the real object... then, you can use the test object to query the real attributes of the real object that it attached to.
Sincel you're clearly doing the tutorial, your object repository probably has a Page test object in the heiarchy under the browser object... (and if you had let Intellisense help, it would show you a list of pages to choose from while you type...). If so, you would specify the page object like this:
Browser("Welcome: Mercury Tours").Page("PageObjectNameHere")
If you would prefer to use descriptive programming, you could instead type something like:
Browser("Welcome: Mercury Tours").Page("Title:=Welcome: Mercury Tours")
Changing your syntax to either of those constructs would let you proceed with the next part of solving your question - how to get some data from the page...
So, once you have address the page test object correctly, then you can specify a method to get information from it... such as .GetROProperty()
You can choose from many properties for a page... If you examine a page using GUISPY, it pretty much gives you a list of the properties available to query... For example, if you want to check the URL of the page that's displayed, you could specify
Browser("Welcome: Mercury Tours").Page("Title:=Welcome: Mercury Tours").GetROProperty("url")
This, of course returns a value, so you want to do something with it... like assign it to a variable
result = Browser("Welcome: Mercury Tours").Page("Title:=Welcome: Mercury Tours").GetROProperty("url")
(If you do this, you can then add the variable "result" to the watch list... which answers your question.)
or examine it directly in your code
if Browser("Welcome: Mercury Tours").Page("Title:=Welcome: Mercury Tours").GetROProperty("url") = url_to_compare then DoSomething()
I hope this helps to clear up your understanding :)

iup.GetParam in LUA: data validation in the callback function

LUA novice, experimenting with GUI using iup.GetParam using LUA 5.1.
I have a simple use of iup.GetParam (which works fine with a simple callback function testing for OK & Cancel) and am trying to add some simple data validation for the parameters (e.g. testing a parameter for being alphanumeric), but am unsure of the correct approach.
I've searched the reference manual (and for code examples), but drawn a blank so far.
Using the string validation example, if I want to reject the
character entered by the user and display the old value of the
parameter, do I simply return 0 from the callback function, or, do
I also have to reset the value of the parameter to its previous
value before the return? Or is the right approach something
completely different?
In either case, do I have to refresh / update the GUI display with a
separate iup call, or does GetParam handle that for me?
Whatever combination I try, it doesn't appear to work (the parameter happily displays the non-alphanumerics). Debugging shows the validation test and return working as coded, so the advice I'm seeking is to get confirmation of the right approach. Sharing a simple working example would be great.
simply return 0
No, IUP will do everything for you, in this case
Download the "getparam.wlua" from the examples folder, then add to its callback this:
elseif (param_index == 1) then
return 0
You will notice that the integer value is now read-only.

How can I find the page object of the page watir is currently on?

Context:
I'm trying to make reusable step definitions that click on page objects on the current page,
e.g. (cucumber step def follows):
When(/^the user clicks the "([^"]*)" button$/) do |button|
click_button = button.downcase.gsub(" ","_")
#current_page #somehow get current page object on this line
#current_page.click_button
end
Problem statement:
I can't find anything that returns the current page object.
An explanation for why the obvious solution didn't work:
I thought #current_page was already there as something I could use. I looked in the source code for page object, and the variable #current_page does exist. Not sure how to use it if I can...
BTW, in this case, I have a bunch of testers that can write Gherkin but not necessarily step definitions. We are trying to rapidly finish a bunch of regression tests for an in house app with an unchanging interface.
This is somewhat at odds with what page-object is trying to provide.
Page object attempts to provide well named actions for interacting with a specific page. If you are wanting to make something that works in general against any page, it will be much easier to write it with watir-webdriver directly.
That said, I agree that a specification based heavily on implementation like that is likely to change. I also would add that it doesn't add much value. I would only continue down this path if you understand and accept that you are using cucumber as a test templating tool instead of a requirements communication tool.
As Justin Ko mentioned, #current_page gets set when you call the on or visit methods. Its not a good idea to lump something that changes the page object in a step that performs a specific action (in this case clicking a button). You might want a different step that indicates the behavior of the application, such as
the application lands on the <your page> page
Then you're can use the name of the page object class to load #current_page via the on method in that step definition. This also gives the benifit (or curse of having your step having more lower level details) of indicating expected page navigation behavior.

Resources