Initial letters missing while using type() in Cypress - testing

Scenario:
<tr>
<td id="type1">
<div><span></span></div>
</td>
<td id="type2">
<div><span></span></div>
</td>
</tr>
cy.get('#type1').type('Abcd') // skips the initial letters ie,
// it actually types 'bcd' or 'cd'
There's an issue here Missing letters when using type #3817. I can see this issue is resolved, but I'm still facing this issue. Any workarounds?

This question leaves out some vital bits that I would like to clear up.
If you try out the click-trick on that HTML snippet, you will come a cropper.
This is the error
cy.type() failed because it requires a valid typeable element.
A typeable element matches one of the following selectors:
a[href]
area[href]
input
select
textarea
button
iframe
[tabindex]
[contenteditable]
The last two are the only options for this particular HMTL, so you could make it work like this:
cy.get('#type1')
.invoke('attr', 'contenteditable') // make it editable
.type('Abcd')
.should('contain', 'Abcd') // confirm it has the text
.invoke('removeAttr', 'contenteditable') // optionally remove the attribute
.should('not.have.attr', 'contenteditable')

Related

Unable to find correct Target selector to click an element

Please see the image below which shows code of the web page in Developer Tools window and advise me the correct selector for 'Depreciation Models'. Text highlighted in red is constant, however the surrounding text is dynamic. So I tried using contains selector to locate but unsuccessful. I want to avoid XPATH as number of before and after div elements may keep changing.
I am using Selenium IDE hence the C#/Java code for RC/Webdriver won't help much.
Selenium IDE generated target path is this:
css=#dhxId_rgWATog7lC3E_27572|6059|6152 > td.sub_item_text > div.sub_item_text
I tried
css=contains('27572') > td.sub_item_text > div.sub_item_text
but it didn't work.
Kindly suggest. I am stuck. Thanks.
As you tried the Locator Strategy as :
css=contains('27572') > td.sub_item_text > div.sub_item_text
Reasons for not working
From the snapshot of the HTML you have provided, 27572 is not the innerText but partial string of the id attribute.
As per selenium.common.exceptions.InvalidSelectorException with “span:contains('string')”:
The :contains pseudo-class isn't in the CSS Spec and is not supported by either Firefox or Chrome (even outside WebDriver).
Solution
You can use the following xpath as per the existing DOM Tree:
xpath=//tr[#class='sub_item'][contains(#id,'27572')]//td[#class='sub_item_text']/div[#class='sub_item_text'][contains(.,'Depreciations Models')]
I guess this would be the right approach:
<style>
[id*="27572"] > td.sub_item_text > div.sub_item_text{
color:red;
}
<table>
<tbody>
<tr id="dhxId_rgWATog7lC3E_27572|6059|6152" class="sub_item">
<td class="sub_item_icon">
<i class="fa fa-user epc-down-circled-2"></i>
</td>
<td class="sub_item_text">
<div class="sub_item_text"> Depriciations Models</div>
</td>
</tr>
</tbody>
</table>
It will set all elements that have an id attribute value containing "27572" and inside of it.

How to find an item in Selenium WebDriver?

I want to find the following item using Selenium. The value of the class changes whenever there is a change. This is inside a complex page (multiple iframes, and other items loaded dynamically). The only unique id is itemid, which is dynamic value and title combination. If I click on this Action, am getting another new set of complex items. I am new to Selenium. How to do that?
HTML:
<td itemid="xxyyy.as123" title="Actions" nowrap="" class="text-button">Actions <img src="../row.gif"></td>
<td itemid="xxyyy.as123" title="Actions" nowrap="" class="text-button button-active">Actions <img src="../row.gif"></td>
<td itemid="xxyyy.as123" title="Actions" nowrap="" class="text-button button-hover">Actions <img src="../row.gif"></td>
The code I tried:
Find by Xpath
var element=driver.FindElement(By.XPath("html/body/div[id='pageContent']/iframe/#document/ht‌ml/frameset/frame[name='detailsDisplay']/#document/html/body/form[name='tableForm‌']/div[id='divToolbarContainer']/div[id='divToolbar']/div[1][class='toolbar']/tab‌​le/tbody/tr/td[title='Actions']"));
Find by Link Text
var element = driver.FindElement(By.LinkText("Actions"));
Any help would be appreciated.
Try
By.CssSelector("td[title="Actions"]");
By.CssSelector("td[itemid="xxyyy.as123"]");
By.CssSelector("td[itemid="xxyyy.as123"][title="Actions"]")
Create Dynamic CSS Selector.
For Example:
driver.FindElement(By.CssSelector("td[itemid$="xxyyy."]")).Click();
Note: In dynamic Elements, there is always a part of locator wich is fixed. we need to generate the locator using this part.
If fixed part is at starting - Use Carrot Character (^)
If fixed part is at Middle - Use Asterisk sign (*)
If fixed part is at End - Use Doller sign ($)
Finally I was able to achieve it, by using the frame names.
driver.SwitchTo().Frame("content").SwitchTo().Frame("detailsDisplay");
var element = driver.FindElement(By.XPath("//*[#id=\"divToolbar\"]/div[1]/table/tbody/tr/td[1]"));
Thanks everyone.

How to select the drop-down items which is out of tag using xpath

I've tried several xpath's for choosing the dropdown list. But nothing is worked.
Some of the xpath's I used are as follows:
By.xpath("//table/tbody/tr[2]/td[2]/span").click;
Or
By.xpath("/td[2]/span[contains(text(),'NATIONAL IDENTITY DOCUMENT')]").click;
Please find the below html tags, I need to select the value either 'ASYLUM SEEKER PERMIT DOCUMENT' or 'NATIONAL IDENTITY DOCUMENT'.
<tbody>
<tr id="jP2Qrg" class="z-comboitem">
<td class="z-comboitem-img"/>
<td class="z-comboitem-text">
<span class="z-comboitem-spacer"/>
ASYLUM SEEKER PERMIT DOCUMENT
</td>
</tr>
<tr id="jP2Qsg" class="z-comboitem z-comboitem-over">
<td class="z-comboitem-img"/>
<td class="z-comboitem-text">
<span class="z-comboitem-spacer"/>
NATIONAL IDENTITY DOCUMENT
</td>
</tr>
</tbody>
When I try to take xpath using firepath, it is dynamic. Every time the xpath and the id is keep changing. So please suggest the xpath which works.
The text is not inside the span text and it is in second td tag. you can try with small change in your code as given below.
By.xpath("//table/tbody/tr[2]/td[2]").click;
or
By.xpath("//td[contains(text(),'NATIONAL IDENTITY DOCUMENT')]").click;
Can you check this..,
By.xpath("//*[contains(text(),'NATIONAL IDENTITY DOCUMENT')]").click;
By.xpath("//*[contains(text(),'ASYLUM SEEKER PERMIT DOCUMENT')]").click;
Try to use below XPath to match required option:
By.xpath("//td[normalize-space()='ASYLUM SEEKER PERMIT DOCUMENT']").click;
or
By.xpath("//td[.='ASYLUM SEEKER PERMIT DOCUMENT']").click;
As #Murthi said, text node is not a child of span, but td. Note that there are some specifics in locating text nodes
Here is the Answer to your Question:
To click on ASYLUM SEEKER PERMIT DOCUMENT you can use the following xpath:
By.xpath("//td[contains(.,'ASYLUM SEEKER PERMIT DOCUMENT') and not (#class='z-comboitem-spacer')]").click;
Let me know if this Answers your Question.
How about this xpaths,
Updated code
(//td[2])[1]//*[starts-with(#class,"z-com")]; // Selects ASYLUM..
(//td[2])[2]//*[starts-with(#class,"z-com")]; // Selects NATIONAL..
This should work if it doesn't have same class name in the same position.
Old xpath's
driver.findelement(By.xpath("(//td[2])[1]")); // selects ASYLUM SEEKER PERMIT DOCUMENT
driver.findelement(By.xpath("(//td[2])[2]")); // selects NATIONAL IDENTITY DOCUMENT
Find out the solution for selecting the dropdown options.
By using List method, I took the list of options available in that field and selected by using clicking the index value of list.
List<WebElement> Idtype = driver.findElements(By.xpath("//tr[2]/td[2][#class = 'z-comboitem-text']"));
Idtype.get(0).click();

Selenium IDE commnand assertTextPresent not working

I am trying to use the command "assertTextPresent" in the Selenium IDE (Firefox). I configure it this way:
Command: assertTextPresent
Target: <th align="center" abbr="Monday" scope="col" style="background-color:#EFEDF5;font-weight:normal;">Mon</th><th align="center" abbr="Tuesday" (truncated for brevity)
Value: (left empty)
From what I can tell, Selenium is not matching my "target" with the source of the rendered page because of the HTML code in it. So it always fails even though it should pass. How can I make this assert work?
assertTextPresent looks for a value that you specify to be somewhere, anywhere on the page. The value you passed in your target is the text IDE is looking for. It literally is looking for that entire <th> tag as "text" displayed somewhere on the page, which I am assuming it is not displayed.
The correct way to use that function would be to pass the text you are looking for in the target, for example "Mon" or whatever you are looking for. Keep in mind, this is case sensitive. Since this function doesn't care where its looking, you don't specify an actual target. I find in most cases this function to usually not have enough specificity to be reliable.
<tr>
<td>assertTextPresent</td>
<td>Mon</td>
<td></td>
</tr>
I prefer to use assertText or verifyText since it requires a target, allowing you to be more specific where to look for the text.
<tr>
<td>assertText</td>
<td>css=th[abbr="Monday"]</td>
<td>Mon</td>
</tr>
I am not entirely sure the abbr attribute would be a valid selector. There are many ways to target your element, I just use that as an easy example since it was part of your snippit.
if you are testing for a unique text present in a page you can use
Command: assertTextPresent
Target: your search text
here in target you put the text you want to search.
Or
if you want to check for the text present at a position you can use
Command: verifyText
Target: id,name,xpath of the position
value: your search text
Based on what I am seeing here you should be usingassertElementPresent instead of assertTextPresent because you are specifying a element as your target rather than just the text of that <th> element.
If you still want to use assertTextPresent you would simply have "Mon" be your target. Because it's target/argument is what it is searching for.
You likely want 3 parts for an assertTextPresent test:
Command: assertTextPresent
Target: //th[#abbr="Monday"]
Value: Mon
This will look in the cell that contains the attriute #abbr equal to "Monday" and see if the contents (text) is set to "Mon".
To test all the cells you would do multiple assertTextPresent commands one-after-another to test all 7 days.
If you want to go "up a level" you would do something like:
Command: assertTextPresent
Target: //tr
Value: <th align="center" abbr="Monday" scope="col" style="background-color:#EFEDF5;font-weight:normal;">Mon</th><th align="center" abbr="Tuesday" (truncated for brevity)
Normally it is best to try to select an HTML element, like by using a specific attribute, such as id="row_1" or name="first_row". Then your target would be //tr[#id="row_1"] or //tr[#name="first_row"].
Target is the "DOM search syntax" to tell Selenium IDE where to find the text you are looking for. Value is the text to be tested.

Selenium RC link locator cannot find link inside table

I has a page as follow:
<table>
<tr>
<th>Company Name</th>
</tr>
<tr>
<td> What Ever Company</td>
</tr>
</table>
The company name is placed arbitrary in the table, so I can only use the link's text to locate the link:
selenium.click("link='What Ever Company'");
However, it says: ERROR:Element link='What Ever Company' not found.
What is the problem here? Is there any other way to click on the link?
Many thanks.
EDIT
Seem that the problem is I have several links with the same text (my bad). After making the link's text unique, I use selenium.click("//a[contains(text(),'Test Campaign 1756237989')]") and it works.
Could this be because you're forgetting the space at the start of the link?
selenium.click("link=' What Ever Company'");
^
Another possible way of clicking the link, is to use an XPath expression:
selenium.click("//a[contains(.,'What Ever Company')]");
This will match all links with 'What Ever Company' in it.
If you want it more exact:
selenium.click("//a[.=' What Ever Company']");
This will only match if the anchor equals ' What Ever Company'.
Another option is to make the search more specific (i.e. tell the locator this link is always inside a <td> with an <a> inside):
selenium.click("//td[a]/a[contains(.,'What Ever Company')]");
The //td[a] looks for all <td> elements with <a> inside. (Differs from //td/a in that if you look for elements with //td[a][2] you get the second <a> which is inside a <td>, while //td/a[2] on the other hand gets the second <a> of the first <td>.)
EDIT: I thought using . as a reference to text() in the XPath expressions should work, but if it doesn't, try using text() instead.
Try these XPaths:
"//table/tr[2]/td/a"
or
"//a[contains(text(), 'What Ever Company')]"
Should work.