How to compare text in Ranorex using C# ?
I wants to compare text from a website. In this case we have to compare the text.
Specify the correct validation form of the string:
string validationHasToMatch = "Test data";
Add the element to the repository that contains the text needed for comparison. Let's call the repo object:
repo.DomObject.textElement;
Build a validation around them:
if (repo.DomObject.TextElement.InnerText == validationHasToMatch)
{
Report.Success("Texts match!");
} else {
Report.Failure("Texts don't match!");
}
This is just a quick example and a base to build more functionality ontop of.
the easiest way is to use the innertext property on the element from the website. So for instance use the xpath to find the element, get the innertext property and compare that text in c# the same way you would compare any other string
Related
I have been working with selenium and C#, and I am wondering if I can return the text associated with a specific element.
For example, here is the line of HTML that contains what I want to display:
<span class="field">1149156-1</span>
Would it be possible to save the "1149156-1" in a string to use for later?
I have tried the following code, but it returns a strange value, definitely not the value I want it to return:
string testvariable = driver.FindElement(By.XPath("/html/body/div/div[4]/div[2]/form/div[2]/div/table/tbody/tr[2]/td[1]")).ToString();
Hope I provided enough information!
You can just use webElement.Text.
string testvariable = driver.FindElement(By.XPath("/html/body/div/div[4]/div[2]/form/div[2]/div/table/tbody/tr[2]/td[1]")).Text;
On another note -- I recommend using relative XPath syntax instead of absolute, and querying on WebElement attributes such as id, name, and class to get more accurate locators.
For example, the XPath in the example you provided can be re-written more robustly as such:
//table[#id='someId']/tbody/tr[2]/td[1]
I am using Selenium ChromeDriver. I have a problem obtaining the contents of a span as defined in the screen shot below:
I tried:
var text = element.Text;
but it returns an empty string.
The span's text is: "Yes, we can both go."
How can I get the span's text?
Edited to reflect updates in the comments
Because it's a span, which behaves much like a div element, there's no inherent text or value attribute at play--it's a container, and you'll need to grab the value from inside the span container (see this answer and this article). Based on the fact you want the actual string value and not the html, the "textContent" attribute is probably the best bet.
You can use Selenium's "GetAttribute" method to get the "textContent" attribute of the span:
var textValue = element.GetAttribute("textContent");
// do code against textValue
There's no direct get accessor built into the Element class for attributes, so you'll need to pass in the string value for the name of the attribute you want, and it'll return the value if it's present.
I have a Automation Suite, currently testing against Wordpress (a test site to practice against). I am attempting to verify when a user edit's an existing Page they are taken to the correct screen. Previously the following code snippet was working fine, however now the ID mentioned below is no longer present (it was an image).
public static bool IsInEditMode()
{
return Driver.Instance.FindElement(By.Id("icon-edit-pages")) != null;
}
Assert.AreEqual(NewPostPage.IsInEditMode(), "You are not in edit mode");
The HTML I am targeting is...
<h2>
Edit Page
Add New
</h2>
I would like to extract the value of the h2 tag 'Edit Page'. Currently I am also getting the value of the anchor 'Add New', which I need to ignore.
using a CssSelector with "h2:first-child" returns both values.
I think I need to use a regular expression, if anyone has any suggestions to help that would be great.
I attempted doing something similar in JSFiddle but require the C# equivalent
var myString = document.getElementsByTagName('h2')[0].innerHTML;
var newString = myString.replace(/<([^>]+?)([^>]*?)>(.*?)<\/\1>/ig, "");
console.log(newString);
You can also get the parent element's text and remove the child element's text from it:
var parent = Driver.FindElement(By.TagName("h2"));
var child = parent.FindElement(By.TagName("a"));
var text = parent.Text.Replace(child.Text, "").Trim();
You can use StringAssert to verify if the string to check contains the expected string. I think is better because you not need to use regex
Example:
StringAssert.Contains(message, expectedmessage);
I have already looked at the post: Efficient plain text template engine, but it didn't answer my question. It's documentation is more than a little lacking, and I don't see that it does what I'm trying to do.
I'm wondering if you can iterate over a template and fill in the values with a function, whose parameters come from attributes within the template. e.g.:
"The <comparison property='fruit' value='green'> and the <comparison property='bowl' value='big'>."
becomes, after iterating over each variable and passing it to a function,
"The fruit is green and the bowl is big."
I'm trying to generate a css page based upon a JSON object containing appearance settings.
EDIT: I'm wondering if there's a way to get the straight object from JsonConvert.DeserializeObject(). The JObject has a lot of information I don't need.
(I am not sure if this is what you are looking for, but) I guess, you can combine my previous answer (showing the use of JObject.SelectToken) with regex to create your own templating engine.
string Parse(string json, string template)
{
var jObj = JObject.Parse(json);
return Regex.Replace(template, #"\{\{(.+?)\}\}",
m => jObj.SelectToken(m.Groups[1].Value).ToString());
}
string json = #"{name:""John"" , addr:{state:""CA""}}";
string template = "dummy text. Hello {{name}} at {{addr.state}} dummy text.";
string result = Parse(json, template);
Im querying a span element (without class, id, or anything) using csspath in selenium c# but the text attribute is empty.
what's happening and how can I get the text within the span?
So the DOM structure is:
/div/div/ul/li/a/span
Query is:
driver.FindElement(By.CssSelector(".contactActions .iconActions .icon .privacy span"));
You could probably try
driver.FindElement(By.XPath("//div[#id='otherOuterDiv']//span"));
I usually use relative XPaths, because I find it easier to specify them.
Solution is:
string text = driver.FindElement(By.CssSelector(".contactActions .iconActions .icon .privacy span")).GetAttribute("textContent"));