XPATH text set dynamic value base on the language - selenium

For example "//XCUIElementTypeStaticText[#text='Accept All']"
which name 'Accept All' can be changed base on the language, for example, Spanish will change to 'Aceptar todas'
example :
<android.widget.TextView index="0" package="com.android.chrome" class="android.widget.TextView" text="Aceptar todas" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[412,1386][669,1438]" displayed="true" />
In this case how to make sure this XPath works with this condition?

You can pass the language string as parameter.
You didn't mention what language you are using, so I will use Java here, but this can be done with any other language similarly.
Let's say we are passing the language as language string parameter.
String xpathTemplate = "//XCUIElementTypeStaticText[text()='%s']";
String xpath = String.format(xpathTemplate,language);
driver.findElement(By.xpath(xpath));

Can you try something like
//android.widget.TextView[#index='0'] or //android.widget.TextView[0]
here is reference : Xpath for Android element using Appium

Related

Unable to locate the login box by type attribute or xpath

HTML looks like following
<input class="text-input text-input-md" dir="auto" ng-reflect-klass="text-input" ng-reflect-ng-class="text-input-md" type="email" aria-labelledby="lbl-14" autocomplete="off" autocorrect="off" placeholder="" ng-reflect-type="email">
the code fails to find login box...tried by attribute
var email_xpath = "//*[type='email']"
then xpath
var email_xpath = "/html/body/ion-app/ng-component/ion-split-pane/ion-nav/page-login/ion-content/div[2]/ion-list/ion-item[1]/div[1]/div/ion-input/input"
var email = webDriver.findElement(By.xpath(email_xpath))
but still unable to get the element....
===============Updated===============
most of the solutions posted below works with selenium firefox driver. The issue was really with htmlunit driver that i was using in scala. Probably it cannot handle javascript properly. I changed it with firefox driver and your solutions works well. The application being tested is an Ionic app (angular), hence i will have to look for another headless solution later.
//*[type='email'] is not correct XPath. Try below instead:
//*[#type='email']
Note that type='email' predicate means child node with string value 'email':
<input>
<type>email</type>
</input>
While #type='email' means attribute type with value "email"
The previous answer is correct but You can try this also //input[#type='email']
The generic syntax is something like as mentioned below for xpath
// - means relative xpath, can be present anywhere inside DOM
tagName - means html tags like td,tr,span,br,input etc
#- denotes start of attribute name present inside html tag
value - actual attribute value present inside DOM
//tagName[#attribute='value']
You can use any XPath, as some are already mentioned by #Andersson and #zsbappa
some others are
//input[#class='text-input text-input-md' and #type='email']
//input[contains(#type,'email')]
Since you are using WATIR, you don't have to write xpath, write the below code, it would work.
b.text_field(type: "email").set "abc#gmail.com"

What is the suitable replacement for this.__LZtextclip.text in Open laszlo 5.0

I want to know what is the suitable replacement for this line.
this.__LZtextclip.text
I am using this to get the string present in the text node. This works fine in Openlaszlo 3.3 but in 4.9 and 5.0 it's giving a problem
I tried updating it to
this.sprite.__LZtextclip.text
And i am getting an error:
79: Error: Access of possibly undefined property __LZtextclip through a reference with static type LzSprite, in line: Debug.write(this.sprite.__LZtextclip.text);
Any idea why this problem is happening?
If you are trying to access the text content of a text field, why don't you just access the attribute text?
<canvas>
<text name="sample" id="gRead" />
<handler name="oninit">
gRead.setAttribute('text',"HI");
Debug.info(gRead.text);
</handler>
</canvas>
In OpenLaszlo 3.3 there is method getText(), which gives you the same value. Accessing mx.textfield in your code does not work for the DHTML runtime.
Edit: Added information regarding the stripping of HTML tags
The Flash Textfield class flash.text.Textfield provides an API to enable HTML tag content in a Textfield instance. There are two different properties, one called text, the other one htmlText. If you want to directly access the Flash Textfield object of an lz.text instance, it's a property of the display object of the lz.text instance:
// Flash Textfield instance
gRead.getDisplayObject().textfield
// Pure text content
gRead.getDisplayObject().textfield.text
// Formatted text
gRead.getDisplayObject().textfield.htmlText
You should be aware of the fact that Flash automatically adds HTML format to any textstring you set as content. When you do
gRead.setAttribute('text',"HI");
the textfield.htmlText value is
<P ALIGN="LEFT"><FONT FACE="Verdana" SIZE="11" COLOR="#000000" LETTERSPACING="0" KERNING="1">HI</FONT></P>
For the DHTML runtime, the text content is added as the innerHTML of a <div> tag, and there is no standardized API to retrieve the pure text content of a DOM structure for a tag with content. You could write your own function to extract the text content, or use JavaScript functions from existing frameworks - like the jQuery text() function - to achieve the same result for the DHTML runtime.
I guess the reason is that Laszlo started using the Dojo based rich text editor for text input with HTML formatting since OpenLaszlo 4.0 or 4.1.
The best approach to have consistent behavior across runtimes when stripping tags is to do the conversion on the server-side. That's especially needed if you wan to have consistent whitespace treatment in multiline text, since there differences in how browsers treat whitespace. The question how to best strip tags from strings in JavaScript has been answered before on Stackoverflow, e.g. JavaScript: How to strip HTML tags from string?
Here is a cross-runtime example which works in DHTML with Firefox, Chrome, and it should work with IE9+:
<canvas>
<text name="sample" id="gRead" />
<handler name="oninit"><![CDATA[
gRead.setAttribute("text", 'Hello <b>World</b> OL');
Debug.info("gRead.text=" + gRead.text);
if ($dhtml) {
Debug.info(gRead.getDisplayObject().textContent);
} else {
Debug.info(gRead.getDisplayObject().textfield.text);
}
]]></handler>
</canvas>
I found what is the problem. The problem is that i have to declare a variable and have to refer the property from that.
<canvas>
<library>
<text name="sample" id="gRead">
<method name="getTextFrom">
Debug.write("this.text" , this.sprite);
var mx = this.sprite;
Debug.write("this.text" , mx.textfield.text);
</method>
</text>
</library>
<handler name="oninit">
gRead.setAttribute('text',"HI");
gRead.getTextFrom();
</handler>
</canvas>

Variable in an attribute in Struts custom tag

I am trying to use a variable inside a custom Struts tag something like follows -
for(String currentMacro : (List<String>)(request.getAttribute("individualMacros"))) {
name = currentMacro.<some-operation>
<html:mce name = "hmtl_<%= name %>" />
Something like this. But <%=name%> is not replaced with the variable value. It works when I am using the variable with a pure HTML tags.
Is there any any way to accomplish this in this case?
Thanks.
Use JSP EL (assuming JSP 2.0, and you put "name" into scope). You could also check to the if the TLD allows rtexprs.
<html:mce name="html_${name}"/>
But why use scriptlets? There's rarely (ever?) a good reason.
Since we are taking about a custom tag, my guess is that in the TLD file there isn't the rtexprvalue option set to true for that particular tag attribute:
<attribute>
<name>name</name>
<rtexprvalue>true</rtexprvalue>
.......
</attribute>
The rtexprvalue specifies that the attribute value may be dynamically evaluated at runtime.
If set to "false" it means that the attribute has a static value which is evaluated at translation; if set to "true" it means the value can be determined dynamically at runtime. Default is "false".
If the scriptlet does not work, it most likely means rtexprvalue is false. If you don't have the liberty to change that, then expressions won't work on that particular attribute.

How to identify the element name in selenium

The element to match is as follows:
... some html....
<table>
some <tr's>
<tr><td>"caption in a <a> tag"</td><td> result value here </td></tr>
more <tr's>
.... more html
I'm using Selenium IDE but don't see a way to match/capture the result value text anywhere ?
Given
<yyy name="iggy" caption="abcd" level="20"></yyy>
you would want to use
assertAttribute("iggy#caption", "abcd")
This uses the name as a locator and #caption to specify the attribute you want. You could follow it with
assertAttribute("iggy#level", "20")
to check the level attribute. You might use 'validateAttribute' instead of 'assertAttribute'. If you want to capture the attribute value use 'storeAttribute'.

How to trim input field value in struts?

I use Struts v1.3 and have following input form:
In struts-config.xml:
<form-bean name="testForm"
type="org.apache.struts.validator.DynaValidatorForm">
<form-property name="displayName" type="java.lang.String" />
</form-bean>
In validation.xml:
<form name="testForm">
<field property="displayName" depends="required">
<arg key="input.displayName" />
</field>
</form>
How do I trim value of "displayName"? How do I trim values of all "java.lang.String" input fields of the form?
You may have a chance to trim the string right at the moment, the request processor updates the data from the input fields to the form. This was not tested, but what happens when you modify the setter setDisplayName(String displayName) to something like
public void setDisplayName(String displayName) {
this.displayName = displayName.trim();
}
This is not a very good solution, because it migrates logic into a setter.
regards
If you want to use trim for validation purposes, I think the proper way is to create and use your own (or extend an existing) validator for required fields that takes trim into consideration.
For an example, you can use this page: http://struts.apache.org/1.2.4/userGuide/dev_validator.html, the section "Pluggable Validators".
If you want to use trim for trimming the String values before using them in your business logic, you can extend org.apache.struts.validator.DynaValidatorForm and overwrite the methods that retrieve values, like get(String name), getString(String name) and so on. After that, you use your class in the form-bean declarations.
If you don't mind having String manipulation logic in your Form class, you can try the StringUtils methods in Apache's Commons Lang JAR:
StringUtils JavaDoc
This will let you trim your Strings in a number of specific ways, whether you want to trimToEmpty, trimToNull, etc. This means you have access to null-safe methods, which can be useful with some values.
Alternatively try using javascript regexp in the jsp that will trim onfocus or onblur
< html:text name="testForm" property="displayName" onfocus="javascript:this.value=this.value.replace(/^\s+|\s+$/g,'')" onblur="javascript:this.value=this.value.replace(/^\s+|\s+$/g,'')" />