Watir Check Text exists - ruby

I get it not the Watir in conjunction with rspec find my text.
The following code leads to this error.
Code:browser.text.include?("Coverberechnung").should == true
Error1: expected: true got: false (using ==)
Error2: Using should from rspec-expectations' old :should syntax
without explicitly enabling the syntax is deprecated. Use the new
:expect syntax or explicitly enable :should instead. Called from
Maybe I can have a help
URL for the Site: enter link description here

You're looking for an initially-capitalized string (i.e. Coverberechnung), but that string is all-capitalized on the test site (i.e. COVERBERECHNUNG).
Try:
browser.text.include?("COVERBERECHNUNG").should == true
or (using expect syntax)
expect(browser.text.include?("COVERBERECHNUNG")).to be true

I prefer to use
expect(browser.text).to include('coverberechnung')

If I wanted to be indifferent about case I would do something like this:
browser.text.upcase.include?("COVERBERECHNUNG").should == true
or
browser.text.downcase.include?("coverberechnung").should == true
this way you can avoid text comparison that may have varying cases.
also for you last problem #3:
use
expect(browser.text.downcase.include?("coverberechnung")).to be true
they deprecated that version some time ago. so you can give this one a try with no issue.
NOTE: only one caveat is that this will ignore case. As described above.

Or you can just do the following:
fail unless #browser.text.include? 'COVERBERECHNUNG'
Or if you want to target that exact string, you could do the following instead:
#browser.h1(text: 'COVERBERECHNUNG').wait_until_present
This code will raise an exception after 30 seconds (thus, failing your test in the process) if it can't find a header element with the text: 'COVERBERECHNUNG'. You can also override the waiting or polling process by doing the following:
#browser.h1(text: 'COVERBERECHNUNG').wait_until_present(10)
That code will check that h1 element within 10 seconds.

Related

Ruby Capybara Element not found error handling

internet.find(:xpath, '/html/body/div[1]/div[10]/div[2]/div[2]/div[1]/div[1]/div[1]/div[5]/div/div[2]/a').text
I am looping through a series of pages and sometimes this xpath will not be available. How do I continue to the next url instead of throwing an error and stopping the program? Thanks.
First, stop using xpaths like that - they're going to be ultra-fragile and horrendous to read. Without seeing the HTML I can't give you a better one - but at least part way along there has to be an element with an id you can target instead.
Next, you could catch the exception returned from find and ignore it or better yet you could check if page has the element first
if internet.has_xpath?(...)
internet.find(:xpath, ...).text
...
else
... whatever you want to do when it's not on the page
end
As an alternative to accepted answer you could consider #first method that accepts count argument as the number of expected matches or null to allow empty results as well
internet.first(:xpath, ..., count: nil)&.text
That returns element's text if one's found and nil otherwise. And there's no need to ignore rescued exception
See Capybara docs

Ruby equivalent call for verifyEditable selenium command

Using ruby selenium-webdirver, I want to check whether input element is editable or not. To check this, in IDE, we've selenium command - verifyEditable. In Ruby, is there any way(method), we verify whether element is editable?
Have you tried $driver.find_element(:id, "elementID").enabled?? This works as long as you're only checking on an element that you know to be an input field.
In my tests, I just declare a method in the same class/module that hosts the browser control:
def element_enabled?(how, what)
if $driver.find_element(how, what).displayed? && $driver.find_element(how, what).enabled? then
true
else
false
end
rescue Selenium::WebDriver::Error::NoSuchElementError
false
end
And then you can just use the method with:
element_enabled?(:id, "elementID")
Having the foo.displayed? && foo.enabled? prevents your script from running into errors if the element you're trying to enter input into isn't currently visible.
Including the rescue command just makes the method return false if you've asked for an element that does not exist on the page. If you leave out the rescue block, your script will exit and return the Selenium error message complaining that the element does not exist. My preference is to always get a true or false instead (like with verifyEditable), but you may find that you want your script to exit with an error if the element isn't present (like with assertEditable).
disabled = find('input_id')['disabled']
disabled.should_not == 'disabled'
disabled.should_not == 'true' # just to be safe
or with selenium driver you can just go ahead fill it in with some value
fill_in 'input_id_or_label', with: "some input value"
If the field is disabled selenium will complain (but some headless driver would not, for example capybara-webkit):
Selenium::WebDriver::Error::InvalidElementStateError: Element is disabled and so may not be used for actions
However I don't think it's a good idea to use selenium to perform such view test -- it's slow and can be flaky, and results may differ from driver to driver. If the view is rendered with input disabled by default, a view test should do the job; otherwise if it's some javascript that disables the input it should be a simple jasmine/mocha javascript test.

Watir: How to verify text is NOT present in web page

I'm writing a test where I delete a record, and I need to verify that the record is no longer present after I've deleted it. I know how to verify the record text is present in a page, with "browser.text.include?", but is there a way that I can verify that the text is not present instead? I need the test to fail if the text is still present after it's supposedly been deleted.
I've searched but the only hits I get on search tell me how to verify text is present - which is the opposite of what I need.
Thank you very much.
browser.text.include? returns a boolean value (actually a truetype or a falsetype, but this isn't the time for that discussion), so negating it will inverse your search.
do_a_faily_thing if not browser.text.include? "badgers are eating my face"
Feel free to customise for your own needs. PS. This is basically ry's answer, only with face-eating badgers.
Added for historical interest, from Chuck's suggestion:
do_a_faily_thing unless browser.text.include? "face-eating-badger-eating-badgers are eating my face-eating badgers"
Added to show an "else" example:
if browser.text.include? "badgers are eating my face"
do_a_thing
else
phew_no_badgers
end
How about:
!browser.text.include?("my string")
or depending on what testing framework you are using
# RSpec and/or Cucumber
browser.text.include?("my string").should != true
# TestUnit
assert(browser.text.include?("my string") != true)
If the record is wrapped HTML element, using exist? method is the another way to verify existence.
deleted record (sample)
<p class='foo'>bar</p>
check script
p(:class,'foo').exist?

JMeter "if controller" with parameters?

I was reading the JMeter documentation and came across this info box about "If Controllers":
No variables are made available to the script when the condition is interpreted as Javascript. If you need access to such variables, then select "Interpret Condition as Variable Expression?" and use a __javaScript() function call. You can then use the objects "vars", "log", "ctx" etc. in the script.
I don't quite follow this. Does this mean if I want access to a "User Defined Parameter" then I can access it only by writing some JavaScript? The example that follows this box then refers to "${COUNT}"
Could someone clarify the usage of the If Controller, maybe with an example or two?
All these answers are wrong! You need to put the variable reference in quotes, like so:
"${my_variable}"=="foo"
You can simply use something like
${my_variable}=='1'
Sometimes JMeter documentation can be confusing :)
Edit 27 september 2017:
The answer here works but has a very bad performance impact when number of threads exceeds 40.
See below for correct and most performing answer:
https://stackoverflow.com/a/46976447/460802
See:
https://bz.apache.org/bugzilla/show_bug.cgi?id=61675
UNCHECK the CHECKBOX
"Interpret condition as variable expression"
I wasted a couple of hours without unchecking this checkbox. It worked with and without semicolon(;) at the end of the statement. Make sure that you have set the User-Defined Variables before calling the if controller.
All the following variations worked for me in Jakarta Jmeter 1.5
${__javaScript("${HOMEPAGE}"=="Y")}
${__javaScript("${HOMEPAGE}"=="Y")};
"${HOMEPAGE}"=="Y"
"${HOMEPAGE}"=="Y";
If Controller will internally use javascript to evaluate the condition but this can have a performance penalty.
A better option (default one starting from JMeter 4, see https://bz.apache.org/bugzilla/show_bug.cgi?id=61675) is to check "Interpret Condition as Variable Expression?", then in the condition field you have 2 options:
Option 1 : Use a variable that contains true or false. For example If you want to test if last sample was successful, you can use
${JMeterThread.last_sample_ok}
or any variable you want that contains true/false
${myVar}
Option 2 : Use a function (${__jexl3()} is advised) to evaluate an expression that must return true or false.
For example if COUNT is equal to 1:
${__jexl3("${COUNT}"== "1",)}
OR
${__jexl3(${COUNT}== 1,)}
Starting with 4.0, if you don't use the "Interpret Condition as Variable Expression?", a warning in RED will be displayed:
If you'd like to learn more about JMeter and performance testing this book can help you.
God bless the http://habrahabr.ru
Have tried until found these.
Using the quotes was my solution.
As Gerrie said you need to check your variable
${my_var} == 'value'
But be careful with the 'User Defined Variables'
Note that all the UDV elements in a
test plan - no matter where they are -
are processed at the start.
That basically means that you cannot define 'User Defined Variables' inside an 'If Controller'. Take a look to the 'BeanShell' instead.
Replace:
${my_variable}=='1'
with
"${my_variable}" == "1"
if it's string value pass as below and its performance effective
${__groovy("${key}"=="value")}
I have used ${code_g1}== 200 in condition and it worked for me.

How can I extract/parse from this SOAP envelope using Ruby?

I am using a gem which uses soap/wsdlDriver.
When I post I get back a SOAP response and am unable to easily parse it.
This is the response I get back:
#<SOAP::Mapping::Object:0x159e95faf098 {}id="27b907f8-da51-f611-ab02-4c5f88a8ec8
8" {}error=#<SOAP::Mapping::Object:0x159e95fae33c {}number="0" {}name="No Error"
{}description="No Error">>
I need to get the entire value in the id="xxxx"
This is what on get on heroku (note: it works locally). This comes from testing various variations of response.id (where response.inspect is what created the output above)
f"
{}error=#>
response[id]
/disk1/home/slugs/220752_47a08bb_10e7/mnt/app/controllers/sugarcrm_controller.rb
:77: warning: Object#id will be
deprecated; use Object#object_id nil
response.id:
/disk1/home/slugs/220752_47a08bb_10e7/mnt/app/controllers/sugarcrm_controller.rb
:79: warning: Object#id will be
deprecated; use Object#object_id
23891500658740
/disk1/home/slugs/220752_47a08bb_10e7/mnt/app/controllers/sugarcrm_controller.rb
:80: warning: Object#id will be
deprecated; use Object#object_id this
is the contact_id: 23891500658740
events:
Ok, I'm 95% sure that is the output of SOAP::Mapping::Object#inspect and not the actual response. And from that class it looks you use the [] method to pull out attributes.
So if I am reading that right, then it looks like you might want:
response_id = response_object['id']
Though each attribute being prefaced with {} seems pretty odd. So if that is actually part of the attribute name, you may need:
response_id = response_object['{}id']
But that seems pretty strange, and may indicate that the SOAP library you are using is not parsing the response properly.
Disclaimer: I've never used this lib before, and posted this from just perusing the docs... This may or may not be very accurate in the ways using this class is described.
I don't know how to do this with ruby, but may be like this code in javascript.
var t = "<SOAP message........>";
var id = t.replace(/.*id=\"(.*?)\".*/, "$1");
Regex details:
.*id=\"(.*?)\".*
.* = anything, 0 or N times.
(.*?) = anything, 0 or N times, stopping at first match.
$1 = first group, the content inside ().
So, everthing will be replaced by id content.

Resources