I'm looking for a way to get the value of the class (for my example "AAABC") stored in a variable. I tried different key words with the getAttribute method, but none were successful. Key word "class" obviously gave me "gwt-Label", all the other key words gave me "null".
Using getAttribute is not necessary, if you can think of an other elegant way.
Example:
<div class="gwt-Label">AAABC</div>
driver.findElement(By.xpath("//div[#class='gwt-Label']")).getText();
This is solve your issue.
First you need to do the following to get the string from your class object:-
String example = object.toString();
// here in msg you will get the whole string < div class="gwt-Label"> AAABC< /div>
Now you can use approach like below to get your string:-
example = example.substring(example.indexOf(">") + 1);
As per the HTML to retrieve the class attribute you can use the following line of code :
String myClass = driver.findElement(By.xpath("//div[text()='AAABC']")).getAttribute("class");
Related
Hi i am new to groovy and i have an issue that i am facing. Currently i am trying to see the values inside typeCache[alias] which seems to be a hashtable.
protected static Hashtable typeCache = new Hashtable();
logger.error "this is type cache : " + typeCache[alias].get(indx)[1];
when i output the element i get the following result in the logs :-
this is type cache : [com.abcd.util.TypeElement#5dc97ce, com.abcd.util.TypeElement#270a8a6, com.abcd.util.TypeElement#5d421487]
am i able to expose further on the elements to see what is in them and what is it doing ?? i am used to php programming and usually in situations such as this i would do a var_dump is there an equivalent of var_dump in groovy ??
thank you.
Each object has a method, which generates a
public String dump()
Generates a detailed dump string of an object showing its class, hashCode and fields. http://docs.groovy-lang.org/latest/html/groovy-jdk/java/lang/Object.html#dump%28%29
This post contains an example, but make sure to read the comment, as the answer is not corrected as of yet.
I am trying to add a function to a JSONJavaObject and calling it from a control on an xpage.
so far I have:
json = (JsonJavaObject) JsonParser.fromJson(factory, colJson);
String func = "function () { alert('you clicked?'); }";
json.put("onClick", new JsonReference(func) );
In the first line I add key-value pairs from a column in a Notes view.
In the second line I define the function as a string.
In the last line I place the converted string as function in the jsonjava object.
I read about this in the following blog post:
http://camerongregor.com/2016/01/19/doublequoteavoidance/
In the next step I bind the function to e.g. a button control as followed:
<xp:button value="Label" id="button1">
<xp:eventHandler event="onclick" submit="false">
<xp:this.script><![CDATA[obj.onClick]]></xp:this.script>
</xp:eventHandler>
</xp:button>
obj is the respresentation of the JSONJava object in SSJS.
But without success. Anyone know how I can call the function in the object?
I hope I will make sense here, let me know if anything to clarify.
If you are simply trying to dynamically output the client side script of a button event, then you don't need to use JsonReference at all. You can just use a String.
In my blog article I might not have make it clear why I needed to use JsonReference. I was using it in the process of rendering a custom UIComponent, part of this process required generating a Json object client side. To do this I created the JsonJavaObject as you did and then asked it to be turned into a string with the 'toJson' method. My problem was that when I asked the whole object to become a string, every property of that object that was a String, would begin and end with a double quote. I needed to ensure that the properties which were intended to be functions did not begin and end with "". By using the JsonReference the JsonGenerator became aware of my intention not to include these double quotes.
In your case, it looks as though you are just trying to dynamically determine what happens with onClick. To do this you could simply use a String instead of the JsonReference. The inclusion of the 'function() {}' is unnecessary as this will be generated when the event handler is rendered at the end of the page.
For Example here would be the Json Java Object
JsonJavaObject obj = new JsonJavaObject();
String func = " alert('you clicked?'); ";
obj.put("onClick", func);
return obj;
And here would be the button:
<xp:button id="button1" value="Alert Me">
<xp:eventHandler event="onclick" submit="false"
script="#{javascript: myBean.myObject.get('onClick')}">
</xp:eventHandler>
</xp:button>
This should give you the end result of seeing 'you clicked?' alert.
You can also inspect how this has all been generated in the script block near the end of the page using 'view Source' or your favourite web browser developer tools:
function view__id1__id2_clientSide_onclick(thisEvent) {
alert('you clicked?');
}
XSP.addOnLoad(function() {
XSP.attachEvent("view:_id1:_id2", "view:_id1:button1", "onclick",
view__id1__id2_clientSide_onclick, false, 2);
});
Let me know if anything isn't clear, hope it helps!
Does obj.onClick already give you a handle to the function returned by the Java class? If it does then you should be able to call it using the call or apply methods that are available in JavaScript:
obj.onClick.call();
obj.onClick.apply();
More details about those two methods can be found here: What is the difference between call and apply?
I want to verify below text(HTML code) is present on page which as // characters , etc using selenium /jav
<div class="powatag" data-endpoint="https://api-sb2.powatag.com" data-key="b3JvYmlhbmNvdGVzdDErYXBpOjEyMzQ1Njc4" data-sku="519" data-lang="en_GB" data-type="bag" data-style="bg-act-left" data-colorscheme="light" data-redirect=""></div>
Appreciate any help on this
I believe you're looking for:
String textToVerify = "some html";
boolean bFoundText = driver.getPageSource.contains(textToVerify)
Assert.assertTrue(bFoundText);
Note, this checks the page source of the last loaded page as detailed here in the javadoc. I've found this to also take longer to execute, especially when dealing with large source codes. As such, this method is more prone to failure than validating the attributes and values and the answer from Breaks Software is what I utilize when possible, only with an xpath selector
As Andreas commented, you probably want to verify individual attributes of the div element. since you specifically mentioned the "//", I'm guessing that you are having trouble with the data-endpoint attribute. I'm assuming that your data-sku attribute will bring you to a unique element, so Try something like this (not verified):
String endpoint = driver.findElement(
new By.ByCssSelector("div[data-sku='519']")).getAttribute("data-endpoint");
assertTrue("https://api-sb2.powatag.com", endpoint);
How to convert String to By type.
Following is my scenario:
Keep object identification in Properties file in below manner
username=By.id("username")
password=By.id("password")
In the application i would like to retrieve the values like
Properties prop=new Properties();
prop.load("above properties file path")
driver.findelement(prop.getProperty("username")) //Here in eclipse it is complaining saying "The method findElement(By) in the type WebDriver is not applicable for the arguments (String)"
So can somebody help me in this?
I can use like below or some other format, but i want solution for the above
username="//*[#id='username']"
username="username"
driver.findElement(By.xpath(prop.getProperty("username"))
driver.findElement(By.id(prop.getProperty("username"))
You can create one parser method which will return desired locator object something like below:
public static By locatorParser(String locator) {
By loc = By.id(locator);
if (locator.contains("id"))
loc = By.id(locator.substring(locator.indexOf("\"") + 1,
locator.length() - 2));
else if (locator.contains("name"))
loc = By.name(locator.substring(locator.indexOf("\"") + 1,
locator.length() - 2));
if (locator.contains("xpath"))
loc = By.xpath(locator.substring(locator.indexOf("\"") + 1,
locator.length() - 2));
return loc;
}
Can be called in your code in the following way:
driver.findElement(locatorParser(prop.getProperty("username")));
Added logic for id, name, xpath. You can modify the same method to add all the available locators. Hope this helps!
The WebDriver.findElement method accepts only an object parameter of the type By.
The Property.getProperty method returns only a String typed object.
Therefore, this may be what fits your need:
WebElement element = driver.findElement(By.name(prop.getProperty("username")));
You can't force a String typed object into a method that accepts only a By typed object. When you ask Selenium to find a String "username" you have to tell it more than just the string's value.
The method By.[method] you choose all depends on what you are looking for in the page that Selenium is searching. "username" is most likely the "name" (By.name) or "id" (By.Id) of the field you are looking for. The By class refines the search to where you expect the String "username" to be: in a name, id, tag, class, etc. See the By class definition.
Also, take caution as the getProperty method could return a null, and the By methods with throw an IllegalArgumentException if you pass it a null string. So providing a default return value ("") for getProperty is usually safer.
WebElement element = driver.findElement(By.name(prop.getProperty("username", "")));
You have to evaluate the entire expression within the context of existing code. You should framework such as JEXL or expressionoasis
Code below uses JEXL
// Create JexlEngine instance
JexlEngine jexl = new JexlEngine();
// Create Expression object for the string
Expression e = jexl.createExpression(prop.getProperty("username"));
// Create a context. You can pass reference to any object you want to use inside the expression
JexlContext jc = new MapContext();
// Now evaluate the expression, getting the result
driver.findElement((By)e.evaluate(jc));
I think you are trying to implement page object model using properties file. What I would suggest here to use xml file instead of java properties file. for example sample xml for page elements would look like below.
<Pages>
<LoginPage>
<txtUserName by="id">username</txtUserName>
<txtPassword by="id">password</txtPassword>
</LoginPage>
</Pages>
Now you can write methods retrieve nodes from the xml file and node attribute as id,xpath etc... further write methods to find elements using xml node value and attribute. I have used same method in my project as it works great for me.
I am Trying to Use a Java component which has 3 string parameters,
I believe to get the reflection entry point resolver to work for this I need to set the payload to be an array of 3 strings however I am struggling to find how to do this,
Any help would be appreciated.
thanks
I am wanting to pass in an xml template file location, xml xpath and new value for the node which the xpath is returning(these will be Hardcoded as array items of the payload(I assume this is the correct approach but I cannot see how to do this)):
public class XMLParserWithParameters
{
public static Object parse2(String TemplatePath, String XPathQuery, String NewValue) throws Exception
{
No need to create an array, just call the static method from MEL:
#[your.package.XMLParserWithParameters.parse2('a','b','c')]
From your post I guess you are looking for a way to set a String array as payload in the config file.
Try the following way
<set-payload value="#[new String[]{"String1" , "String2", "String3"}]"></set-payload>
Or You can also try the following way.
<expression-transformer>
<return-argument expression="new String("Test")" />
<return-argument expression="new String("Test3")" />
<return-argument expression="new String("Test5")" />
</expression-transformer>
In the expression you can use any of the Mule Expressioin Landuage expression.
Hope this helps.
I have found that this can be achieved using an expression transformer by setting multiple return values which will create an array of strings.
If you are trying to set Array of String as Payload simply this should work