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

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?

Related

How to use formula CASE WHEN on Netsuite Oracle?

Hi Can someone help me with the Saved Search: So I’m trying to create a formula(numeric) that would say:
CASE WHEN {internalid} = ‘32319’ THEN 1 ELSE 0 END
— didn’t work it didn’t even show on the description
I also tried:
CASE {internalid} WHEN 32319 THEN 1 ELSE 0 END — also didn’t work and didn’t show on the description.
Not sure if perhaps using formula on my saved search is disabled on my end if so how can I know or check? Or maybe I did not use to correct syntax for the CASE WHEN? The formula is also grayed out(highlighted).
I managed to find a solution. As it turns out I just needed to put a value of 1 on the textbox for Value to validate the CASE WHEN Statement. Also whether there's a single quote for the numbers or in this case without a single quote, it still worked.
Either of those forms should work but your screen shots show a Formula(Text) rather than Formula(Numeric)

How to get a specific viewable string on page through Nokogiri

Currently, I am able to parse a website with Nokogiri and grab specific elements from pages. However, I need to be able to grab a specific string such as "Out of stock" that is visible to the user:
page.text.match('Out of stock')
That works fine for grabbing the correct string and returning true or false if the string is or isn't there, however, some links like the following, return true even if the item is not out of stock because that specific string is hidden in a script tag on the page:
https://www.walmart.com/ip/Funyuns-Onion-Flavored-Rings-6-oz/36915849?athcpid=36915849&athpgid=athenaItemPage&athcgid=null&athznid=PWSFM&athieid=v0&athstid=CS020&athguid=ba634528-888-172187cc96a580&athancid=null&athena=true
I am looking for a way so that that string is pulled if and only if it is visible to users so the above should return false for matching the "Out of stock" string, while the link below should return true (at time of posting), because the item is actually out of stock.
https://www.walmart.com/ip/4-Pack-Chesters-Flamin-Hot-Popcorn-4-25-oz/737202470?selected=true
I am also aware that I could grab the specific tag that contains the string, but I need to monitor hundreds of websites so the solution has to be a broad search for a visible string.
short answer: we can use xpath syntax for this with more specific.
long story:
I strongly recommend to put more specific with css-classes, coz, in some of the cases we can get this text not only in "script tag" but also by media query or in item-preview blocks or whatever, and handle common cases as big chunks, but not to force to use one specific solution for all cases, in case of unexpected behavior
so we need to be more specific and use the "target-tags" to handle it, for example:
Nokogiri::HTML.parse(page.html).xpath("//*[contains(#class, 'prod-PriceSection')]//*[contains(#class, 'prod-ProductOffer-oosMsg')]").text
"Out of stock"
so, "to monitor hundreds of websites" we can going with this approach:
xpath("//*[contains(#class, 'PriceSection')]").text
or even better to use something like this to be sure that element is surly visible:
page.all("//body//*[contains(text(), 'Out of stock')]", visible: true).count
# => 1
if the usage of one more request (in previous solution) by Capybara may become a problem, we can follow with this solution, it's much faster:
xpath("//body//*[not(self::script) and contains(text(), 'Out of stock')]").count
I hope it's help

Proper way to assert text in Selenium Webdriver/RSpec

So im writing a framework in pure selenium-webdriver and am curious what the proper way to assert text exists (Such as in an alert message for instance on an invalid login for example). Specifically with RSpec.
I can think of two ways that comes to mind. Doing something like so:
text_to_check = driver.find_element(locator).text and then doing something like expect(text_to_check).to be("Bad Login text") The locator in this case would probably be xpath or css locator I guess? Although I feel like xpath would probably make more sense (Im not super familiar with xpath tbh though)
Use the driver.page_source() and then check against that....but that seems brittle if that text exists somewhere else on the page. Also it seems unnecessary to do that and pull in the whole page source to check what is essentially one element.
String expected = “abc.com;
String actualURL= “abc.com”;
Assert.assertEquals(expected, actualURL);
message – Message to be displayed in case of an Assertion Error.
condition – Condition against which the assertion needs to be applied.
Assert.assertTrue(“Assert True test message”, true);

#driver.find_element(:id=>"body").text.include?(textcheck) not verifying the text only the id

I am using Selenium-WebDriver for Ruby and I am trying to verify that text is present on a page. I have done many searches and tried many things and the best answer I have found is to use something like
def check_page(textcheck)
if verify {#driver.find_element(:id=>"body").text.include?(textcheck)}
yield it_to "fail"
else
yield it_to "pass"
end
end
The expected outcome if the value of textcheck is present in the body would be pass and if the value of textcheck is not present in the body it would be fail. What is actually happening is if :id=>"body" is present then it is pass and if it is not present then it is fail regardless of .text.include?(textcheck)
If anyone could point me in the right direction for how to verify text is present on a page using Selenium-WebDriver in Ruby it would be greatly appreciated. I have found workarounds for certain cases where I can do
verify {#driver.find_element(:tag_name, 'h1').text!=(textcheck)}
but the element I am trying to verify I can't get to so easily. I looked into css locators and was very confused on how to simplify the tag so I could use it. Any help would be greatly appreciated. Thank you very much. If you require any more information from me please let me know and I will provide it as soon as possible.
I am using Ruby 1.93 with Selenium-WebDriver 2.25 testing in Firefox 14.0.1
I do it this way
#wait = Selenium::WebDriver::Wait.new(:timeout => 30)
begin
#wait.until { #driver.find_element(:tag_name => "body").text.include?("your text")}
rescue
puts "Failure! text is not present on the page"
#Or do one of the options below
#raise
#assert_match "true","false", "The text is not present"
end
UPDATE
Answer to your question in the comments section.
There are two kind of "waits", implicit wait and explicit wait. You can read more about it here. The reason your code failed was because you were searching by "id"=>"body" and not by "tag_name"=>"body". Usually all text is encompassed within the "body" HTML tags in your DOM.

How can I use Nokogiri to find specific text/words on a webpage?

I am new to nokogiri, but it looks like this would be the tool that I would use to scrape a webpage. I am looking for specific words on a webpage. The words are "Valid", "Requirements Met", and "Requirements Not". I am using watir to drive through the website. I currently have:
page = Nokogiri::HTML.parse(browser.html)
to get the html, but I am not sure where to go from here.
Thanks for the help!
If you are using Watir to drive the website, I would suggest using Watir to check for the text. You can get all the text on the page using:
ie.text #Where ie is a Watir::IE
You could then check to see if it has those words are included (by comparing to a regex):
if ie.text =~ /Valid|Requirements Met|Requirements Not/
#Do something if the words are on the page
end
That said, if you are looking for a specific bits of text, you can use Watir to look specifically for those elements (and avoid parsing text or html). If you can provide an HTML sample of what you are working on, we can help find a more robust solution.
I am not sure why you are using both. You could get the page using 'net/http' or mechanize if you just want to check for text. Anyways, you can check for text in watir with browser.text.match 'Valid', same for nokogiri with page.text.match 'Valid'.
You should also be able to use the .text method from Justin's answer along with the standard ruby string .include? method which returns true or false.
if browser.text.include? /Valid|Requirements Met|Requirements Not/
#code to execute if text found
else
#code to execute if text not found
end
This also makes it easy to have a single line validation step if that is what you are after
if using rspec/cucumber
browser.text.should include /Valid|Requirements Met|Requirements Not/
if using test:Unit
assert browser.text.include? /Valid|Requirements Met|Requirements Not/

Resources