Besides Xpath, how do I capture custom attributes - ruby

I'm trying to capture the date of an object but the object does not have id or name attributes. Also there are multiple items with the class of "ng-binding" so I don't think I can use that.
Is there a way for me to capture values from objects using custom attributes? Meaning is there a way for me in Ruby to say
varObject = find_element(:ng-binding-html, "$ctrl.app.publishedDate")
The object I'm trying to capture is
<span ng-bind-html="$ctrl.app.publishedDate" class="ng-binding">11/20/2017</span>
I took a look at an older post which seems to be close to the same issue I'm having but I was sure.
Selenium webdriver : how to find the element in DOM based on custom attribute
Thanks,
Scott

Is there a reason why you don't want to use XPath? The Ruby Selenium WebDriver find_element method that I think you refer to in your question accepts XPaths as an argument.
You could use the following XPath:
"//span[#ng-bind-html='$ctrl.app.publishedDate']"
Your Ruby code could be:
varObject = #driver.find_element(:xpath,"//span[#ng-bind-html='$ctrl.app.publishedDate']")

Related

How to pass hash values into fields in registration form?

I am new to automation testing and have no experience in Ruby, nor any other programming language. However my current employer wants from me to start automating small tasks. The problem I stumbled upon is when trying to pass hash values into fields of a registration form.
This is my Gherkin Scenario:
On the left hand side of the when-statement is the name of the field in which I want to pass a certain value. The first two are input fields. The last hash "age" is a dropdown.
Can someone help me out by giving me an example ruby method, that would fill-in such a registration form, given the information above?
P.S. The automation framework I use consists of Ruby, Cucumber and Watir Webdriver.
The following code is untested, but something like this should work for you.
# Step definition
Given /^I enter the following credentials$/ do |table|
# .drop(1) is to get rid of your first row with titles
table.drop(1).each_pair do |setting, value|
browser.element(name: setting).to_subtype.set(value) }
end
end
Be carefull using Gherkin as a replacement for coding. It should be focussed on what you're trying to achieve, not how you're trying to achieve it.
I would recommend a scenario as follows:
Given RegularJohn is on the create account page
When he submits the create account form with his information
Then the successfully created message should display
Note that in your code you would need RegularJohn to be a configuration variable that is filled with the expected information information.
When you try to test a failure you could use a different variable, like BadEmailAndy. This way you keep it readable, maintainable and understandable.

Custom child commands for UI elements

We're currently using Select2 for all our <select> elements and for obvious reasons cy.get("#element").select("foo") won't work. As per the docs we've created our own custom command cy.get("#element").select2("foo") which just wraps a number of steps to select "foo".
This works well when trying to select a value but we'd like to avoid adding custom commands for clear(), value(), etc.
We'd also like to avoid using the Select2 API because this would require someone to understand that API as opposed to just working in the Cypress API.
We'd like to create something a bit more flexible by instead having a Cypress friendly object. Something that would look like this:
cy.get("#element").select2().as("select2");
cy.get("#select2").select("foo");
cy.get("#select2").should("be.equal", "foo");
cy.get("#select2").clear();
Is it possible to create a child-command that returns an object that can then override these built in commands?
We're unable to return our custom object from our custom command without first cy.wraping it which defeats the purpose.

Extracting a link with jmeter

So I need to delete an "onclick" dynamic link using jmeter.
Here is the sample of one of the links:
"Delete"
What I need is to extract number and post it in order to do the delete action. Every link is the same except the number.
I have tried to implement some of the solutions I've found on this site but it didn't work.
Thanks in advance
Peace
If you need to do it with XPath you could try going for substring-after function like:
substring-after(//a[text()='Delete']/#href,'param=')
The above expression returns everything which is after param= text in href attribute of a HTML tag having Delete text.
You can test your XPath expressions against actual server response using XPath Tester tab of the View Results Tree listener.
References:
substring-after Function Reference
XPath 1.0 Language Reference
Using the XPath Extractor in JMeter
XPath Tutorial

Obtain XML element's value from REST server response using Ruby

n00b REST question. I'm making a GET request to an API's endpoint and getting the proper XML response. The question I have is, how do I get the value of a particular XML element in the servers REST response using Ruby?
So let's say one of the elements is 'Body' and I want to assign its value 'Blah blah blah' to a variable
Part of the XML response:
<Body>Blah blah blah</Body>
How would I do that with the response? Basically I want to do something like this
variable = params["Body"]
Thanks in advance!
The best solution is to use RestClient or HTTParty and have it parse the response for you.
Otherwise, you'll have to parse the response itself using a library such as Nokogiri:
doc = Nokogiri.XML(response)
variable = doc.at("body").text
You'll want to use an XML parser of some kind.
It sounds like you want something like XmlSimple, which will turn an XML document into ruby arrays and hashes. There's tons of examples of how to use it on the page that has been linked.
One thing to be aware of is that XML to native container mappings are imperfect. If you're dealing with a complex document, you'll likely want to use a more robust parser, like Nokogiri.
If you want full XML Object Mapping, HappyMapper is a decent library, although it isn't very active anymore. It can work with XML from any source, so you'll still want something like the libraries mentioned by #Fitzsimmons or #MarkThomas to do the HTTP request.

How can I get Mechanize objects from Mechanize::Page's search method?

I'm trying to scrape a site where I can only rely on classes and element hierarchy to find the right nodes. But using Mechanize::Page#search returns Nokogiri::XML::Elements which I can't use to fill and submit forms etc.
I'd really like to use pure CSS selectors but matching for classes seems to be pretty straight forward with the various _with methods too. However, matching things like :not(.class) is pretty verbose compared to simply using CSS selectors while I have no idea how to match for element hierarchy.
Is there a way to convert Nokogiri elements back to Mechanize objects or even better get them straight from the search method?
Like stated in this answer you can simply construct a new Mechanize::Form object using your Nokogiri::XML::Element retrieved via Mechanize::Page#search or Mechanize::Page#at:
a = Mechanize.new
page = a.get 'https://stackoverflow.com/'
# Get the search form via ID as a Nokogiri::XML::Element
form = page.at '#search'
# Convert it back to a Mechanize::Form object
form = Mechanize::Form.new form, a, page
# Use it!
form.q = 'Foobar'
result = form.submit
Note: You have to provide the Mechanize object and the Mechanize::Page object to the constructor to be able to submit the form. Otherwise it would just be a Mechanize::Form object without context.
There seems to be no central utility function to convert Nokogiri::XML::Elements to Mechanize elements but rather the conversions are implemented where they are needed. Consequently, writing a method that searches the document by CSS or XPath and returns Mechanize elements if applicable would require a pretty big switch-case on the node type. Not exactly what I imagined.

Resources