Fail to use statement IF in Selenium (With Ruby)

Currently I’m trying use a statement IF (If a button appears in the
page,
then run the IF), see the method Login in the system:

If the button doesn’t appear in the page, I would like to run the next
method Remove and add new expense

require “selenium-webdriver”
require “rspec”
require “rspec/expectations”

describe “#Add simple expense and after add a receipt”, :suitee => true
do

before(:all) do
@driver = Selenium::WebDriver.for :chrome
@base_url = “http://sitetest.com
@driver.manage.window.maximizeend

it “Login in the system” do
@driver.get(@base_url)
@driver.find_element(:id, “user_email”).send_keys “[email protected]
@driver.find_element(:id, “user_password”).send_keys “123456”
@driver.find_element(:name, “commit”).click
if(@driver.find_element(:css,
“.btn.btn-lg.btn-success.btn-block”).displayed?)
@driver.find_element(:css,
“.btn.btn-lg.btn-success.btn-block”).click
@driver.find_element(:css,
“.introjs-button.introjs-skipbutton”).click
@driver.find_element(:css, “.i.i-pencil”).click
endend

it “Remove and add new expense” do
begin
while(@driver.find_element(:css, “.i.i-pencil.icon”).displayed?)
button = @driver.find_element(:id, “expense-bulk-select”)
@driver.action.click(button).perform
delete = @driver.find_element(:id, “delete-multi-btn”)
@driver.action.click(delete).perform
waitDisplayModal = Selenium::WebDriver::Wait.new(:timeout => 10)
waitDisplayModal.until {@driver.find_element(:class =>
“bootstrap-dialog-footer-buttons”)}
@driver.find_element(:xpath, “//div[3]/div/div/button[2]”).click
sleep 3
end rescue Selenium::WebDriver::Error::NoSuchElementError
@driver.find_element(:id, “current_expense_merchant”).send_keys “Taxi
to work”
@driver.find_element(:id, “current_expense_amount”).send_keys “50”
@driver.find_element(:id, “button-add-expense”).click
waitDisplayIconTrash = Selenium::WebDriver::Wait.new(:timeout => 20)
waitDisplayIconTrash.until {@driver.find_element(:css =>
“.i.i-pencil.icon”)}
endend

after(:all) do
@driver.quit
endend

My problem: When I run this script, appears this in my console:

Failure/Error: if(@driver.find_element(:css,
“.btn.btn-lg.btn-success.btn-block”).displayed?)
Selenium::WebDriver::Error::NoSuchElementError:
no such element
(Session info: chrome=42.0.2311.135)
(Driver info: chromedriver=2.9.248304,platform=Linux
3.13.0-24-generic x86_64)

That is, the IF is not working as I would like. How can I fix it?

CHeers

On Wednesday, May 27, 2015 at 7:11:54 PM UTC+2, Rafael s wrote:

@driver.find_element(:id, “user_password”).send_keys “123456”
button = @driver.find_element(:id, “expense-bulk-select”)
@driver.find_element(:id, “button-add-expense”).click

Failure/Error: if(@driver.find_element(:css,
“.btn.btn-lg.btn-success.btn-block”).displayed?)
Selenium::WebDriver::Error::NoSuchElementError:
no such element
(Session info: chrome=42.0.2311.135)
(Driver info: chromedriver=2.9.248304,platform=Linux 3.13.0-24-generic
x86_64)

That is, the IF is not working as I would like. How can I fix it?

It means that the driver cab’t fins the CSS element you pass in, -

.btn.btn-lg.btn-success.btn-block

in your case.

On Wednesday, May 27, 2015 at 7:11:54 PM UTC+2, Rafael s wrote:

@driver.find_element(:id, “user_password”).send_keys “123456”
button = @driver.find_element(:id, “expense-bulk-select”)
@driver.find_element(:id, “button-add-expense”).click

You can check the displayed page by adding the below statement just
before
your IF statement:

save_and_open_page

One more thing is to check if you enable selenium ddriver in your rspec
config file:

Capybara.default_driver = :selenium

Hello Javix,

Thanks for reply. Well, I’m using only selenium + rspec, no capybara.

I want to just make one condition: IF find the button…clicks, else
continues execution of the other methods

Em quinta-feira, 28 de maio de 2015 05:05:09 UTC-3, Javix escreveu:

On 28 May 2015, at 14:24, Scott R. [email protected] wrote:

On May 28, 2015, at 6:15 AM, Rafael s [email protected] wrote:

I want to just make one condition: IF find the button…clicks, else continues
execution of the other methods

If the button doesn’t exist, you’re calling displayed? on it anyway.
@Scotte:
An element can exist but be hidden, that’s what #displayed? method
checks according the API docs:
def displayed?
bridge.isElementDisplayed @id
end
The problem with the search by CSS is that you can have them more than
one on a page. What if you try to find the same button by id ? Just add
one your HTML code to see what will happen.

Hey Scott,

I’ve tried without displayed but appears the same error.

Em quinta-feira, 28 de maio de 2015 09:24:20 UTC-3, Scott R. escreveu:

The error points it out clearly that the element does not exist on the
page. Try to check the html CSS code to compare with one you test.

Sent from my iPhone

On May 28, 2015, at 6:15 AM, Rafael s [email protected] wrote:

I want to just make one condition: IF find the button…clicks, else continues
execution of the other methods

If the button doesn’t exist, you’re calling displayed? on it anyway.


Scott R.
[email protected]
http://www.elevated-dev.com/
https://www.linkedin.com/in/scottribe/
(303) 722-0567 voice

Sent from my iPhone

On 28 May 2015, at 22:09, Rafael s [email protected] wrote:

but that’s exactly what I want! I know that sometimes the element will not
appear on the screen, so I need it to run another method, it is exactly this
treatment I am trying to do. for this reason I put the IF condition. If the
element exists, takes an action, orexists, performs the following method. Just to
be clear, how would you do this?

As I suggested, I’d change CSS to an ID and see what it will give.
Then I would check if there is no typo in CSS you of the button under
test.
If there is still a weird behaviour I would try another gem, e.g.
capybara, phantom JS, etc.
Take a try and come back with your results.

but that’s exactly what I want! I know that sometimes the element will
not appear
on the screen, so I need it to run another method, it is exactly this
treatment
I am trying to do. for this reason I put the IF condition. If the
element
exists, takes an action, orexists, performs the following method. Just
to
be clear, how would you do this?

Em quinta-feira, 28 de maio de 2015 11:10:14 UTC-3, Javix escreveu:

Rafael,

*Login in the system … and … **Remove and add new expense *are NOT
methods.

In RSPEC they are test code blocks and they will always run. You can
create
methods in your code using

def foo
end

def bar
end

it ‘run my tests’ do
if condition
foo
else
bar
end
end

By they way you have a typo here: :suitee => true

-Wale
railsfever.com