Geb changing an attribute value - selenium

I want to know what is the correct way to change any attribute value of an element in geb (selenium).
For example, I have this element:
static content = {
radioSelect(wait: true) { $('input[name=rr]')}
}
Now I want to set any attribute. I did not found a good method, I use this workaround:
(driver as JavascriptExecutor).executeScript( "document.getElementsByName('rr')[0].setAttribute('checked', true)" )
Is there another way? Like radioSelect.setAttribute?

Use the jQuery integration offered by Geb:
radioSelect.jquery.attr("checked", true)
This is the equivalent of
js.exec 'jQuery("input[name=rr]").attr("checked", true)'
But be careful:
The jQuery integration only works when the pages you are working with
include jQuery, Geb does not install it in the page for you. The
minimum supported version of jQuery is 1.4.

Related

Is #FindBy (id = 'blabla') same thing as //*[#id='blabla]

I decided to give a try to Katalon and got this question because Katalon Studio, when user choose "locate by attribute 'id' " it shows that //*[#id='blabla'] will be used to locate element. But as for me it looks like variation of xPath. So i wonder, when in JAVA - Selenium i write #findBy (id='blabla') - under the hood selenium convert it to //*[#id = 'blabla'] ???
Could`t find answer
Google, forums, katalon docs
UPDATE:
I want to know if this two methods are exactly the same. 1. Will it take same amount of time to find element? 2. Are both of methods going to start looking for element from top of the DOM ?
Functionally these all do the same thing:
The #FindBy(id = "foo") annotation
driver.findElement(By.id("foo"))
driver.findElement(By.xpath("//*[#id = 'foo']"))
driver.findElement(By.css("#foo"))
They all find an element by its Id.
I'm not sure what that translates to in API calls to the Selenium Server, but Selenium is open source. Have a look for yourself.
The #FindBy(...) annotation is defined in FindBy.java, which inherits from AbstractFindByBuilder. Trace through the code and you'll figure it out.
It is the equivalent. (But it could depend on the particular Selenium bindings)
If to look into By class of Selenium Java library there you can see the code like this:
public List<WebElement> findElements(SearchContext context) {
return context instanceof FindsById ? ((FindsById)context).findElementsById(this.id) : ((FindsByXPath)context).findElementsByXPath(".//*[#id = '" + this.id + "']");
}
A single element lookup just reuses lookup of a collection.

How to find webelement using style property

Below is the HTML code of an element and I want to locate this element by classs and style property using selenium webDriver in java
<div class="qooxdoo-table-cell" style="left:252px;width:117px;height:24px;"/>
suggest a way which can be help full in selenium
I want to locate the element using java code
i.e. Driver.findelement(by. ....
As long as the element isn't unique you must grab both attributes:
This is the general form, replacing the empty strings for your required class and style:
driver.findElement("By.xpath(//div[#class='' and style='']");
So:
driver.findElement(By.xpath("//div[#class='qooxdoo-table-cell' and style='left:252px;width:117px;height:24px;']");
Best of luck!
If you need to match <div> with exact style attribute, you can try something like
driver.findElement(By.xpath("//div[#class='qooxdoo-table-cell'][#style='left:252px;width:117px;height:24px;']"))
Another way is to use cssSelector as follows:
driver.findElement(By.cssSelector("div[style='left:252px;width:117px;height:24px;']"));
If you are using a older version of selenium you can as well use the older version of cssSelector.
driver.find_elements_by_css_selector("div[style='left:252px;width:117px;height:24px;']")
Note: this function still works in current version of Selenium (with DeprecationWarning )

Dojo attach point / byId returns undefined

I made a template and there is a <select dojotype="dijit.form.ComboBox" dojoAttachPoint="selectPageNumber" id="selectPageNumber">tag with id and dojoAttachPoint be "selectPageNumber". I want to populate it with options upon create so I add some code to the postCreate function:
var select = dijit.byId("selectPageNumber");
or
var select = this.selectPageNumber;
but I always have select being undefined.
What am I doing wrong?
UPD:
The problem with element has been solved spontaneously and I didn't got the solution. I used neither dojo.addOnLoad nor widgetsInTemplate : true, it just started to work. But I have found the same problem again: when I added another tag I can't get it!
HTML:
<select class="ctrl2" dojotype="dijit.form.ComboBox" dojoAttachPoint="selectPageNumber" id="selectPageNumber">
</select>
<select class="ctrl2" dojotype="dijit.form.ComboBox" dojoAttachPoint="selectPageNumber2" id="selectPageNumber2">
</select>
widget:
alert(this.selectPageNumber);
alert(this.selectPageNumber2);
first alert shows that this.selectPageNumber is a valid object and the this.selectPageNumber2 is null.
widgetsInTemplate is set to false.
all the code is within dojo.addOnLoad()
dojo.require() is valid
I am using IBM Rational Application Developer (if it is essential).
WHY it is so different?
Based on your syntax, I am assuming that you are using 1.6. Your question mentions template and postCreate, so i am assuming that you have created a widget that acts as a composite (widgets in the template).
Assuming 1.6, in your widget, have you set the widgetsInTemplate property to true. This will tell the parser that your template has widgets that need to be parsed when creating the widget.
http://dojotoolkit.org/documentation/tutorials/1.6/templated/
I would remove the id from the select. Having the id means that you can only instantiate your widget once per page. You should use this.selectPageNumber within your widget to access the select widget.
If you are using 1.7 or greater, instead of setting the widgets widgetsInTemplate property, you should use the dijit._WidgetsInTemplateMixin mixin.
http://dojotoolkit.org/reference-guide/1.8/dijit/_WidgetsInTemplateMixin.html
Depending on when dijit.byId() is being called, the widget may not have been created yet. Try using dojo.addOnLoad()
dojo.addOnLoad(function() {
var select = dijit.byId("selectPageNumber");
});
I came close to the solution: it seems like there is a some sort of RAD "caching" that doesn't respond to changes made in html code.
Ways to purge the workspace environment with RAD (based on Eclipse) might be a solution.

zend framwwork 2 inArray element validator boring

How to disable the inArray validator of Zend\Element\Select ?
I can not remove this standard validator select element.
Edit:
What I'm trying to do is populate a select element so dynamic with ajax. So that way the inArray loses the reference field value.
Does anyone know what is the right way to populate this element with ajax?
It actually does not look like it is possible at this point in time to disable the validator; however, you can override the select element to be able to remove the validator for this specific case:
use Zend\Form\Element\Select;
class MySelect extends Select {
public function getValidator() {
return $this->validator;
}
}
Basically the key issue with the current select element is that if the validator does not exist; it will create it. The other option you have here is to set a validator manually; which you should likely be doing is manually creating an InArrayValidator and populating it with the potential options that would be coming from your AJAX call. In which case you would need to add a setter above.
Since version 2.2, Zend Framework provide the ability to disable inArray validator calling:
$element->setDisableInArrayValidator(false);
or passing option to an element:
'disable_inarray_validator' => false

JUI Autocomplete html-encoded suggestions

jQuery UI starting from version 1.8.4 html-encodes Autocomplete suggestions (according to this issue).
This became a problem for me now. I used to theme the output for the suggestions, but now (if I use version 1.8.4 or higher) Autocomplete just html-encodes my theming. All tags like <b>, <span> are being printed to the user instead of displaying the actual styling.
So the suggestions now look like:
<b>su<b>suggestion
another <b>su<b>suggestion
instead of:
suggestion
another suggesion
I've read about custom data, but I use Yii framework and the output is being generated from certain actions (PHP code).
So, how do I theme the output now?
Thank you!
Better use a HTML plugin
You can use open function from jQuery UI to replace the encoded text.
Here's an example:
$this->widget('zii.widgets.jui.CJuiAutoComplete', array(
'name'=>"bug",
'source'=>$this->createUrl('/autocomplete'),
// additional javascript options for the autocomplete plugin
'options'=>array(
'open'=> 'js:function(event, ui){
$("ul.ui-autocomplete li a").each(function(){
var htmlString = $(this).html().replace(/</g, "<");
htmlString = htmlString.replace(/>/g, ">");
$(this).html(htmlString);
});
}'
),
));