Set and get checkbox state

Ask general questions here.
mrt
Posts: 35
Joined: Mon Mar 16, 2020 11:31 am

Set and get checkbox state

Post by mrt » Tue Jun 30, 2020 9:52 am

Hello all,

I have a problem regarding getting and setting the state of a checkbox.
This is needed conditionally, so I have to do it in User Code anyway.

I found this:
https://www.ranorex.com/forum/how-to-ch ... t6087.html

I tried to use the code from there, but I have some problems with it.
My code:

Code: Select all

public static void SetCheckboxState(RepoItemInfo webelementInfo_checkbox, string checkboxState)
{
	// Instantiate repository
	MyRepo repo = MyRepo.Instance;
	
	//create the checkbox from the repository  
	Ranorex.CheckBox chkbox = repo.Projects.Workflow.ET.Checkbox_1;
  
	//Show the checked state
	var test = chkbox.Checked;
}
But i get a compile error on the middle line saying:
Cannot convert type 'Ranorex.WebElement' to 'Ranorex.CheckBox'

What am I doing wrong?

Usually I instantiate my Elements with

Code: Select all

WebElement checkbox = webelementInfo_checkbox.CreateAdapter<WebElement>(true);
but this WebElement has no checked property which I could get/set.

The checked attribute is not there when unchecked, and appears when checked, like here: https://www.w3schools.com/tags/att_checked.asp
Regarding the DOM, the checkbox looks like the following:

unchecked:

Code: Select all

<custom-checkbox><span>Checkbox 1</span></custom-checkbox>
checked:

Code: Select all

<custom-checkbox checked><span>Checkbox 1</span></custom-checkbox>
repo path:

Code: Select all

body//div/tag[@tagname='custom-checkbox']/span[@innertext='Checkbox 1']/ancestor::tag[@tagname='custom-checkbox']
Any help is appreciated, thank you!

BR
mrt

User avatar
odklizec
Ranorex Guru
Ranorex Guru
Posts: 6163
Joined: Mon Aug 13, 2012 9:54 am
Location: Zilina, Slovakia

Re: Set and get checkbox state

Post by odklizec » Tue Jun 30, 2020 12:31 pm

Hi,

Could you please post a Ranorex snapshot (NOT screenshot) of the problematic checkbox and ideally, also full HTML code for given checkbox?
As far as I know, web checkboxes belong to Input tag, so I think you must use Ranorex.InputTag, instead of Ranorex.CheckBox, to control web-based checkbox element.
Pavel Kudrys
Ranorex explorer at Descartes Systems

Please add these details to your questions:
  • Ranorex Snapshot. Learn how to create one >here<
  • Ranorex xPath of problematic element(s)
  • Ranorex version
  • OS version
  • HW configuration

mrt
Posts: 35
Joined: Mon Mar 16, 2020 11:31 am

Re: Set and get checkbox state

Post by mrt » Tue Jun 30, 2020 12:51 pm

Yes, maybe I am on the wrong path with checkbox, but the UI element only has the <custom-checkbox> tag, there is no input or checkbox tag (also nowhere in ancestors, siblings or children.
I guess if I can cast the custom tag to some sort of InputTag or Checkbox, it maybe would work, but I have no idea how to achieve this.

Regarding the snapshot, I am afraid I can't, because this snapshot is huge and contains senstive data, and I cannot censor it.

Is there maybe another solution how to provide information?
I could take screenshots from spy, from tree, from parent elements with everything expanded, whatever...
I can do anything where I have control which information goes into "the cloud".

I know it would be easier to help if a snapshot is provided, but it would be really great if we could find another solution.

thank you

User avatar
odklizec
Ranorex Guru
Ranorex Guru
Posts: 6163
Joined: Mon Aug 13, 2012 9:54 am
Location: Zilina, Slovakia

Re: Set and get checkbox state

Post by odklizec » Tue Jun 30, 2020 1:00 pm

Hi,

I'm afraid, that without, at very least, snapshot or exact html code (including style) behind the checkboxes, there is not much anyone here can do or suggest. The HTML code you posted is simply not enough to provide a reasonable solution. Looking for a solution from screenshots is pain in the a... ;) So I think your best hope is to contact directly Ranorex support and request a remote session with them?
Pavel Kudrys
Ranorex explorer at Descartes Systems

Please add these details to your questions:
  • Ranorex Snapshot. Learn how to create one >here<
  • Ranorex xPath of problematic element(s)
  • Ranorex version
  • OS version
  • HW configuration

mrt
Posts: 35
Joined: Mon Mar 16, 2020 11:31 am

Re: Set and get checkbox state

Post by mrt » Tue Jun 30, 2020 1:52 pm

Ok, I got access to a demo page which does not contain sensitive data.
Hopefull that will help

It is not the exact same code as in my previous post (regarding the span and label placements inside the control), but this shouldn't be a big issue.

I copied the HTML from DOM browser, if you want any other format I will provide it.

thank you, BR
Attachments
demo.txt
(5.79 KiB) Downloaded 2 times
checkbox-snapshot.rxsnp
(341.62 KiB) Downloaded 2 times

User avatar
odklizec
Ranorex Guru
Ranorex Guru
Posts: 6163
Joined: Mon Aug 13, 2012 9:54 am
Location: Zilina, Slovakia

Re: Set and get checkbox state

Post by odklizec » Tue Jun 30, 2020 2:16 pm

Hi,

Thanks for the snapshot and html code. Well, I'm afraid, you are out of luck here. The checkboxes you are working with are not exactly checkboxes, but a mess of tag elements, pretending to be checkboxes ;) Hence the code you are tried is completely useless.

At first, you should try to identify the checkboxes with most recent Ranoex 9.3.2 (even in trial mode). You are using an outdated Ranorex version, which is unfortunately not helpful in your situation. However, 9.x may not help either, because as I mentioned, the checkboxes are not exactly checkboxes. I'm afraid, there are not "checked" attributes found by Ranorex spy. At least I did not find any of them? Is the sample page available publicly? I was not able to access it via url found in the snapshot... https://vizbox.avl.com/usage/controls/a ... ox/samples
Pavel Kudrys
Ranorex explorer at Descartes Systems

Please add these details to your questions:
  • Ranorex Snapshot. Learn how to create one >here<
  • Ranorex xPath of problematic element(s)
  • Ranorex version
  • OS version
  • HW configuration

mrt
Posts: 35
Joined: Mon Mar 16, 2020 11:31 am

Re: Set and get checkbox state

Post by mrt » Tue Jun 30, 2020 2:32 pm

That's sad to hear. :(
Thank you for looking into anyway :)

No sorry, the sample page is just for internal use.

I guess I made a mistake and created the snapshot while the checkbox was unchecked, but the only difference when changing states I noticed, is a attribute "checked" without any value.

Like stated above, the attribute is not there at all when unchecked (I guess that's the case in the snapshot),
and it appears when checked, but has no value.

I attached a quick screenshot of Spy, you can see it under the Dynamic section. This is for checked state.


Yes, I know my version is outdated, but I couldn't find out if the license for 8.3 will still work with v9 so I was afraid to uninstall and reinstall (and maybe rollback) unless there is a very good reason.
Attachments
2020-06-30 15_28_44-Editing 'BoxReference.checkbox'.png
2020-06-30 15_28_44-Editing 'BoxReference.checkbox'.png (13.97 KiB) Viewed 158 times

User avatar
odklizec
Ranorex Guru
Ranorex Guru
Posts: 6163
Joined: Mon Aug 13, 2012 9:54 am
Location: Zilina, Slovakia

Re: Set and get checkbox state

Post by odklizec » Wed Jul 01, 2020 6:38 am

Hi,

I think that using Checked=false for actually 'enabled' checkbox, is somewhat quirky solution and I believe that the so called "checkbox" is poorly implemented? Honestly, I don't understand what are such crazy UI elements good for and why there are not use standard HTML input checkboxes?

Anyway, what you can try is to read and evaluate the ID of checkbox ;) It seems that ID of checked checkbox is always 'checkedIcon', while unchecked says 'outlineIcon'. So basically, to get the state of checkbox, you can use for example xpath like this:

Code: Select all

/dom[@domain='vizbox.avl.com']/body[@class='avl-body']//div[@class='sideNavAndContent']//iframe[@src='https://vizbox.avl.com/node_modules/@avl-controls/core/dist/avl-checkbox/demo/avl-checkbox.demo.html']//tag[#'checkboxWithLabel']//label/span[#'icon']/div
And then simply get the value of ID of found checkbox. checkedIcon means that the checkbox is checked.
As for setting the checkbox, you will have to simply mouse click the element, instead of using SetValue action, which is a "no go" for this kind of "checkbox". Hope this helps?
Pavel Kudrys
Ranorex explorer at Descartes Systems

Please add these details to your questions:
  • Ranorex Snapshot. Learn how to create one >here<
  • Ranorex xPath of problematic element(s)
  • Ranorex version
  • OS version
  • HW configuration

mrt
Posts: 35
Joined: Mon Mar 16, 2020 11:31 am

Re: Set and get checkbox state

Post by mrt » Wed Jul 01, 2020 6:51 am

ok thank you.

Yes, it is like in n my first post.

When the box is unchecked, it is:

Code: Select all

<custom-checkbox ...> My Checkbox Label</custom-checkbox> 
and when checked it is:

Code: Select all

<custom-checkbox checked ...> My Checkbox Label</custom-checkbox> 
This seems to be standard HTML, at least for types <input>: https://www.w3schools.com/tags/att_input_checked.asp

But I guess as this is no input, Ranorex don't recognizes it as such.
For unchecked, Ranorex does not know at all about the checked attribute.
For checked, Ranorex sees a checked-attribute, but without value set and defaults to false I guess.

For testing, if I put a <input> element on the page with the same behavior regarding this checked property

Code: Select all

<input checked ...> My Label 
Ranorex has no difficulties and it all works flawlessy.

So in fact, if Ranorex would ignore the tag-name and "just do so, as if it was a input-tag", i would get it to work I guess.
But how to tell Ranorex, that the custom-type is of type InputTag?

thanks, BR mrt

User avatar
odklizec
Ranorex Guru
Ranorex Guru
Posts: 6163
Joined: Mon Aug 13, 2012 9:54 am
Location: Zilina, Slovakia

Re: Set and get checkbox state

Post by odklizec » Wed Jul 01, 2020 7:10 am

Hi,

Well, because the HTML behind the checkbox says <custom-checkbox> and not <Input>, I don't think it can be "remapped". Simply, it's an HTML tag, which Ranorex don't understand and therefore, takes it as TAG and not as Input. Therefore, you must implement your own handling of such checkbox.
Pavel Kudrys
Ranorex explorer at Descartes Systems

Please add these details to your questions:
  • Ranorex Snapshot. Learn how to create one >here<
  • Ranorex xPath of problematic element(s)
  • Ranorex version
  • OS version
  • HW configuration

mrt
Posts: 35
Joined: Mon Mar 16, 2020 11:31 am

Re: Set and get checkbox state

Post by mrt » Wed Jul 01, 2020 7:13 am

Ok, thank you, I will try :)

mrt
Posts: 35
Joined: Mon Mar 16, 2020 11:31 am

Re: Set and get checkbox state

Post by mrt » Wed Jul 01, 2020 7:36 am

Hmm ok, with checking for the ID checkedIcon and outlineIcon basically works...
but you are right, it is far from ideal solution

User avatar
odklizec
Ranorex Guru
Ranorex Guru
Posts: 6163
Joined: Mon Aug 13, 2012 9:54 am
Location: Zilina, Slovakia

Re: Set and get checkbox state

Post by odklizec » Wed Jul 01, 2020 7:39 am

Hi,

Well, it's probably the best you can do with given checkbox ;)
Pavel Kudrys
Ranorex explorer at Descartes Systems

Please add these details to your questions:
  • Ranorex Snapshot. Learn how to create one >here<
  • Ranorex xPath of problematic element(s)
  • Ranorex version
  • OS version
  • HW configuration

orianajonathan
Posts: 1
Joined: Fri Jul 10, 2020 8:34 am

Re: Set and get checkbox state

Post by orianajonathan » Sat Jul 11, 2020 7:55 am

I don't think I can "improve" that, because the HTML in the back of the checkbox is "input, not <custom-checkbox>. In short, this is an HTML tag that Ranorex does not understand, so it is translated as a payment, not a product, so you need to process the check box.