How to test XELement and its child elements? - fluent-assertions

I have a sample Xml code snippet
<modification name="givenName" operation="add" xmlns="urn:oasis:names:tc:DSML:2:0:core">
<value>Changed name</value>
</modification>
The xml is loaded to my XElement, and I used
XElement xml = ...to load xml above...;
xml.Should().HaveAttribute("name", "givenName")
.And.HaveAttribute("operation", "add")
.And.HaveAttribute("xmlns", "urn:oasis:names:tc:DSML:2:0:core")
.And.HaveElement("value");
to test my code, the attributes testing are all passed, but the element testing (the last condition) fails.
Anyone can point out what is wrong with my code?
And how can I test the Xml has an element named "value" and its value is "Changed name"?
Thanks in advance!

I suspect the problem is that the XName of the element isn't just value - it's value with a namespace. Presumably HaveElement is namespace-aware. Try this:
XElement xml = ...to load xml above...;
XNamespace ns = "urn:oasis:names:tc:DSML:2:0:core";
xml.Should().HaveAttribute("name", "givenName")
.And.HaveAttribute("operation", "add")
.And.HaveAttribute("xmlns", "urn:oasis:names:tc:DSML:2:0:core")
.And.HaveElement(ns + "value");
The last line checks whether it's got the namespace-qualified element.

It's going to be part of Fluent Assertions 2.1. If you can't wait you can get it through the Git repository

Related

How to select one from duplicate tag in page in java in selenium webdriver

I am using Selenium WebDriver and I have number of items on a page and each item on page is a separate form type.
I have saved all of these form elements in a list and I am iterating over every item in an attempt to get the name of the element by using the "alt" attribute.
However when I try to get the "name" attribute from the input element it is always returning the first input tag found on that page, not the name attribute of the element I have currently selected.
The syntax I am using is:
((Webdriver imgtags.get(i)).findelement(By.xpath("//input[#name='qty']")).sendKeys ("100");
I have also tried to get the id from the tag by using:
((Webdriver imgtags.get(i)).getAttribute("id");
It's returning a blank value, but it should return the value of the id attribute in that input tag.
I also tried to get the id by using .bytagname but as id is an attribute it is not accessible
Try:
(driver) findElement(By.xpath("//*[contains(local-name(), 'input') and contains(#name, 'qty')]")).sendKeys("100");
To answer the comment by #rrd: to be honest, I have no idea why OP uses ((Webdriver imgtags.get(i)). I don't know what that is. Normally, I just use driver.findElement[...]
Hoping that he knows what works in his framework :D
Selenium Xpath handling is not fully compliant and it does not always treat // as a synonym of descendant-or-self.
Instead try tweaking your code to use the following Xpath:
((Webdriver imgtags.get(i)).findElement(By.xpath("./descendant-or-self::input[#name='qty']")).sendKeys("100");
This will base your search off the currently selected WebElement and then look for any descendants that have a name attribute with a value of "qty".
I would also suggest storing your imgtags array as an array of WebElement e.g.
List<WebElement> imgtags = new ArrayList<>();
This is a much better idea than casting to WebDriver to be able to use .findElement(). This will cause you problems at some point in the future.

Getting Attribute using xpath in selenium

I have been looking for an XPath code to get the value of the attribute of an HTML element as part of my regression testing. Can anyone please help
the attribute value is dynamic changes for every next webelement
below is the HTML code
<figcaption id="recentVideosvideoname0" data-videono="xZA6FJ32Twe2GQYEuBHJnQ==" title="test for test" class="caption">test for test</figcaption>
i want attribute : data-videono
I have tried something like this
By.xpath(("//div[contains(#id,'recentVideos')]/div/a/figure/figcaption"));
Here is the Answer to your Question:
To retrieve the attribute data-videono you can consider to use the following line of code:
String my_attribute = driver.find_element_by_xpath("//figcaption[starts-with(#id, 'recentVideos') and #title='test for test']").getAttribute("data-videono");
Let me know if this Answers your Question.
Please follow the approach mentioned below:
driver.findElement(By.xpath("//div[contains(#id,'recentVideos')]/div/a/figure/figcaption")).getAttribute("data-videono");
You can get attribute value by using XPath itself.
XPath 1.0
"//div[contains(#id,'recentVideos')]/div/a/figure/figcaption/#data-videono"
Notice the #data-videono at the end of string, which will return attribute value (p.s. it seems like it is base64 encoded data)
I don't know if i got your question, but do you already tried:
String var = driver.frindElemnt(By.xpath("put your xpath in here")).getAttribute("data-videono");

not able to find element using id in findelement in selenium

I am not able to find the element using "id" in selenium as the id is randomly changing in every session of execution so the same id i am not getting in next execution.
As there is no other unique property is there to identify the element.
code sample
You didn't specify a language so I'm going to give you Java. You can do this by using the CSS class or probably a better choice (because of likely uniqueness) is data-lynx-name.
By CSS class
driver.findElement(By.cssSelector("div.http-lynx-json-org-text-input"));
By attribute
driver.findElement(By.cssSelector("div[data-lynx-name='username']"));
You really should read the question that I duped this one to:
Find element by attribute
Also read more about CSS selectors,
http://www.w3.org/TR/selectors/#selectors
You can use XPath.
String xpath = //div[#data-lynx-name='usernameLabel'][text='User ID']/following-sibling::div[1]
The above XPath will will find the div tag containing text 'User ID' and finds the next div which is is the required textbox.
It seems that you can even use the attribute 'data-lynx-name' attribute of the textbox div tag directly.
String xpath = //div[#data-lynx-name='username']
Selenium
driver.findElement(By.xpath(xpath));

Xpath query not working in Objective C using GdataXML

I am trying to parse an xml using GdataXML
I have a query which is not working and I wonder why:
<address_component>
<long_name>Mars</long_name>
<short_name>Mars</short_name>
<type>route</type>
</address_component>
I want to save the long_name where the type is route and I am using this query
/*[name() = 'address_component']
/*[name() = 'type' and text() = 'route']
/*[name() = 'long_name']
What am I doing wrong here?
===========
Is there a way for GDataXML to parse the xml line for line?
is there a way other than running the xml a few times into an array?
I want to save the long_name where the
type is route
This XPath expression should work:
/address_component[type='route']/long_name
Meanning: a long_name element child of an address_component root element having a type child element with 'route' string value.

What does the dojo.query() return?

I'm just getting started with dojo, and I've understood that dojo.query is the same as $ in jQuery.
But I haven't figured out what it returns. Is it a specialized object like in jQuery?
What I'm trying to do (with no luck) is:
dojo.query("output").innerHTML = data;
//this doesn't work either:
dojo.query("output").html(data);
//tried accessing by id as well
dojo.query("#output").html(data);
//and tried to access a div, incase dojo has some issues with html5 elements
dojo.query("#divOutput").html(data);
And I'm currently using the new html5 elements:
<output id="output">Output goes here</output>
<div id="divOutput">non-html5 output goes here</div>
And I can't seem to find a good list on what to do with objects returned by dojo.query()..
edit: Okay, I think dojo is just messing with me now. I found this method: addContent() and that works on the above selector. But I don't want to add content, I want to replace content...
The query method returns a NodeList object.
In the ref for NodeList you can find a list of functions that you can apply to the list
of elements. There is no innerHTML function for the list, but the html function should work.
There is no "output" element in HTML, perhaps you try to target elements with the class name "output"?
dojo.query(".output").html(data)
Or the element with id "output"?
dojo.query("#output").html(data)
If you want to replace all the output tags' content with the same thing, then this code should always work:
// replace the contents of ALL <output> tags
dojo.query('output').forEach(function(node) { node.innerHTML = data; });
Dojo also provides a little shortcut for these kinds of things. You can specify a string to NodeList's forEach function like this:
// replace the contents of ALL <output> tags (as long as data is global)
dojo.query('output').forEach("item.innerHTML = data;");
The word item in the string is special. (This is a pain to debug, so it might not be worth it.)
As was said above, query method returns NodeList object, so you can iterate it's result as array, or use dojo methods that work with NodeList (e.g. attr):
dojo.query("#divOutput").attr("innerHTML", data);
But as soon as you are trying to query nodes by id, it would be better to use dojo.byId() method, which returns domNode:
dojo.byId("divOutput").innerHTML = data;
Or in more dojo style:
dojo.attr(dojo.byId("divOutput"), "innerHTML", data)
Try this by adding the [0] like this:
dojo.query("output")[0].innerHTML = data;
Also, there is a dojox.jq wrapper (in development, coming in 1.4) which emulates the JQuery return object APIs
The documentation seems to be a mess, this is the only thing i get to work with 1.7,
dojo.query("whatever").forEach(function(node, index, array)
{
node...
});