WebElement clear function doesn't clears the input values in some cases - geb

I'm setting up a geb automation front-end test which requires me to clear the input values and set new values. I use WebElement clear method but it works for some input element but doesn't for some.
I use following code as a work around which may be a poor hack
def length = InputElement.value().toString().length()
while(length-- > 0){
InputElement.firstElement().sendKeys(Keys.BACK_SPACE)
}
Is there any better way to achieve the same functionality?

You should be able to set the input.value = ""

Related

clicking on dynamic values in a web table in selenium

Below is my Table structure:
I want to click on the first cell of "policyno " column only if value is not empty
How do I achieve this?
Since you didn't specified, I have no idea what programming language do you need, but the idea is the same for all. I've used C# for the code below:
Get all the policy number elements. This can be easily achieved by getting the elements by xpath. In your case, I expect the lblPolicyNumber to be present in all:
IList allPolicyElems = driver.FindElements(By.Xpath(".//*[contains(#id,'lblPolicyElements')]"));
Now, you have 2 options to click on the element you need. First, you click on an element using his position in the list: allPolicyElems[0].Click(); (not the best way to do it) or by using LINQ (or lambda expressions for Java) to get the element by the text (perhaps you have the text stored in a variable from some previous actions): allPolicyElems.FirstOrDefault(t => t.Text == "your_text_here").Click();
This can be further expanded by applying the same logic in case you need to get the correct element by knowing other cell values from the same row.
Try using this.
WebElement elem = driver.findElement(By.xpath("//table/tbody/tr[2]/td[3]/span/a"));
if(!(elem.getText().trim().equals(""))){
elem.click();
}

Selenium automation with xpath for all browsers

I am new to selenium automation and I am trying to figure out how to handle the code for an element that shows different xpath for different browsers.
//xpath for IE
private readonly By ByPermIE = By.XPath("//*[#id='group-permissions']/div[1]/div[2]/span[2]");
//xpath for chrome
private readonly By ByPermChrome = By.XPath("//*[#id='group-permissions']/div[1]/div[1]/span[2]");
If I am performing some action on this element on different browsers how do I use these elements in the test case. I am planning to extend this to three more browsers. So, should I use if else conditions everywhere? Is there any alternate of best practice for such cases?
You can use below xpath for you case.
private readonly By ByPermChrome = By.XPath("//*[#id='group-permissions']/div[1]//span[text() = 'text that span contains']");
You can write xpath as relative as possible.It is not advisable to write absolute xpaths.
For your above case use below xpath which works in both the browsers.
By.XPath("//*[#id='group-permissions']//span[2]");
or
By.XPath("//*[#id='group-permissions']//span[text()='someThing']");
Refer this for more info regarding xpath.
if u post ur html code, than it will be easy. But as there is no html given, i can suggest u a way like below:
//u can use if condition here
if(driver.findElements(By.XPath("//*[#id='group-permissions']/div[1]/div[2]/span[2]")).size()==1){
//perform operation here
}
else
//perform operation here
This is an alternative way as there is no html code.
If u give the html code, i think we can suggest u more be
One suggestion I can give here is you can use OR conditions in xpaths. Say OR OR .
Xpath OR returns successful as soon it hits the first correct one.

Copy-paste using Capybara?

I would love to do something like this:
div = find '#some-div'
copy_to_clipboard(div)
input = find '#my-input'
paste_from_clipboard(input)
I do not want to simulate this with send_keys and using Ctrl+C and Ctrl+V; I want this to work cross-browser (especially on mobile).
Does this API exist?
The most simple way I've found:
element.send_keys [:control, 'c']
element.send_keys [:control, 'v']
There is no Capybara copy/paste API - If all you want to do is copy the visible text into an input then you could do
div_text = find('#some-div').text()
find('#my-input').set(div_text)
If that's not correct for what you want, then you could use #execute_script to create a selection range like
var range = document.createRange();
range.setStart( <start node>, <start node character offset> );
range.setEnd( <end node>, <end node character offset> );
window.getSelection().removeAllRanges();
window.getSelection().addRange(range);
then find your target element and set it's value to window.getSelection().toString(). Note that's not really emulating what a user would do, so if you are actually using this for testing an app I would still recommend using the ctrl/cmd-c/v after setting the selection range for browsers that support it since it emulates user behavior better.
It's an old one, however you don't need to use capybara however the workaround would to use this incredibly simple gem:
https://github.com/janlelis/clipboard
There is no API to do it.
You can get element from one browser
div = page.find('#some-div')
Then you can pass it to another browser
fill_in '#some-other-div' with => div
You can read more about capybara here: https://github.com/jnicklas/capybara

selenium ide having problems with dynamic id

i am testing our web based application using selenium. I am having problem in a button which has dynamic id and the class is similar to the last html page so i am unable to go ahead with the testing. below is the source of the button
input id="aui_3_4_0_1_554" class="addto_cart_button" type="button" onclick="chkMaxRequestPerDay();" value="Request Quote">
I want to know how can i tell selenium ide to check with value so that it can proceed
Thanks
you can try using xpath with value,
//input[#value='Request Quote']
or
//input[#value='Request Quote' and #class='addto_cart_button']
I think something like below should also work
//input[contains(#id, 'aui_')]
or
//input[#class='addto_cart_button']
this will give you a List
loop through them and in the loop check
loop over List<Webelement> {
if( webelement.getAttribute("onclick").indexOf("chkMaxRequestPerDay") != -1) {
// here is the element. do what ever you want
}
}

Selenium: How to enter white space as the value of a text field?

I have a form text field that is being populated with a default value. I would like to clear the value and enter white space to assert that the expected validation occurs. I am using Selenium RC and the PHPUnit Selenium extension. How can I achieve this?
Note: I have already tried doing these, but they do not work:
$this->type('FirstName', " "); //spaces
$this->type('FirstName', "\t"); //tab character
They clear the value from the field, but they do not enter the white space into the field.
Update: I found that if you are using Selenium IDE, you can type white space by using their ${space} variable but this does not work when using Selenium RC and the PHPUnit Selenium extension.
recently I am having the same problem just like Andrew where the ${space} isn't work in Selenium RC. I found out that Zugwalt's solution is useful. Here is what I did to capture the whitespace.
selenium.focus("txt_textbox1");
selenium.keyPressNative("32");
strOutput = selenium.getEval("this.browserbot.findElement('txt_textbox1').value = ' ';");
Hope this help ;)
THanks #!
I fixed this in the phpunit-selenium code on github and made a pull request to the maintainer.
Here is my gist if anyone cares:
https://github.com/undernewmanagement/phpunit-selenium/commit/1b783ba8cfe4736f525b316a75a991e0a701afd1
You could use javascript injection and manipulate the value directly
selenium.getEval("this.browserbot.findElement('FirstName').value = ' ';");
Note this will not fire off any events so you would have to manually trigger them if desired.
How about setting it's value to an empty string ('')?
How about using the keyPress() function to "press" the spacebar?
You can try selenium.typeKeys('FirstName'," ");
Please let me know if it worked.
This should work for phpunit with Selenium:
class MyTest extends PHPUnit_Extensions_Selenium2TestCase
{
public function testSomething()
{
$textField = $this->byCssSelector('.blah');
// ...
$textField->clear();
$textField->value(" ");
// test assertion to follow
}
}
If you want to enter a sentence, the following won't work:
$textField->value("this is a sentence.");
The following will:
$textField->value("this");
$textField->value(" ");
$textField->value("is");
$textField->value(" ");
$textField->value("a");
$textField->value(" ");
$textField->value("sentence.");