Problem with monitor resolution in Capybara object identification - ruby

When I run on a 15″ monitor, capybara is not able to click on a specific menu item, but when I run on a 29″ monitor, the test works perfectly.
How to adjust this in a way that the test runs regardless of monitor size?
I am using the following configuration:
Capybara.default_driver = driver
Capybara.default_max_wait_time = 30
Capybara.page.current_window.resize_to(1366, 768)
Capybara.page.driver.browser.manage.window.maximize

You can resize the window to width: 2000, height: 2000 and see if works.
Or Your element might not be visible to click. So, you can add to scroll to that element and click it
For that, add a separate method like below
def scroll_to_css(css_selector)
script = "document.querySelector('#{css_selector}').scrollIntoView(true);"
Capybara.current_session.evaluate_script(script)
end
And call before clicking the element ex: scroll_to_css ".css_selector"

Related

Ruby: how to renew the path of an image after image has been changed?

I am trying to build a GUI with ruby Shoes to select a background image for my desktop. As I am struggling with a particular part of my idea I am going to describe just the problem part, the rest works fine.
Here's the code of my ruby Shoes app:
Shoes.app :title => "Images!" do
stack do
#img1 = image "desktop_pic", :width => 200, :height => 100
button "Change image", :margin => 25 do
#img1.path = "/home/njutik/preview_desktop_pic"
end
end
end
Here's what the result looks like when I start my Shoes app:
result_at_beginning
In the background there is a different ruby script running which generates a new background-image and stores it as preview_desktop_pic.
So when I click the Change image-button, the path of #img1 gets adjusted and I see the new image:
result_after_clicking_change_button
That's fine so far. My problem is, that nothing happens when I click the Change image-button again. Of course in the meantime there is already a new image preview_desktop_pic so the code line
#img1.path = "/home/njutik/preview_destkop_pic"
which is executed each time I press the Change image-button should show me a new picture but nothing happens. Even when I delete the preview_desktop_pic from the folder and then press the Change image-button there is still no change at all and all I see is the same picture shown after clicking the Change image-button for the first time.
So my question is: what am I doing wrong and how can I make the Shoes app show the current preview_desktop_pic every time I press the Change image-button?
Any hints would be really great!
Update: After the comment of 7stud I tried to define a singleton method for the button. Like this:
#change_image = button "Change image"
def #change_image.click
#img1.path = "/home/njutik/preview_login_background"
end
#change_image.click
But that did not help - nothing changed.
Then I tried this:
def #img1.reload
#img1.path = "/home/njutik/preview_login_background"
end
button "Change image" do
#img1.reload
end
But this also did not help. I thought that by defining singleton methods I would delete the cache memory.
Any further hints would be really helpful.
I don't think you are doing anything wrong. I think that what you are seeing has to do with image caching, which Shoes uses for efficiency. If ten windows all use the same image, Shoes does not fetch the file from disk (or download the file from the web) for each window. Instead, Shoes caches the image used in the first window, and when the other windows use the same path for the Image, Shoes retrieves the image from the cache. As a result, if the path for your Image doesn't change, it looks like Shoes uses the cached image. It would be nice if an Image in Shoes had a reload() method, which would force Shoes to ignore the cache and go get the image again.
In your app, the first button click works because the Image's path changes from "/desktop_pic" to "/home/njutik/preview_desktop_pic". Subsequent clicks do not change the Image's path.
To change the image, the new image must have a different name than the previous one. I do not know why but it works for me then.

Selenium ActionDriver.click not working in Firefox

I've got a test where I need to use the action click. I can't directly click the element I need because it's a ::before element, and the element it's before is 0x14 so it's not interactable. I'm using watir-webdriver instead of selenium directly, which is why wd is used. It accesses Selenium's objects instead of Watir's.
I have an element, I am going to use the parent to find the location I need to click.
$browser.wd.action
.move_to(element.parent.wd)
.move_by(12, 0)
.click
.perform
This code works fine in Chrome, however nothing gets clicked in Firefox. Both zooms are set to 100%. Relative to the parent, the object I need to click is in the same place.
I don't know where else to look.
I've tried this with: Selenium 2.42 on Firefox 29 and Selenium 2.44 on Firefox 34.
Thanks in advance.
UPDATE
I've downgraded to FF 31.1.1 and have changed my Driver instantiation code to include native events:
if $BROWSER == :firefox
profile = Selenium::WebDriver::Firefox::Profile.new
profile.native_events = true
return Watir::Browser.new $BROWSER, profile: profile
else
return Watir::Browser.new $BROWSER
end
No luck with click.
I'm trying it on a website where I can see mouse movement and clicking: http://www.escapemotions.com/experiments/flame/. It's an interactive drawing program. The mouse moves just fine, click_and_hold seems to work,and drag_and_drop_by works fine too. I run into trouble on click.
Chaining click_and_hold.release works. I'm going to submit a defect for 2.44.
Seems to be a defect. As a work around click_and_hold and release can be chained to simulate a click.
driver.action.click_and_hold.release.perform
The defect is logged: https://code.google.com/p/selenium/issues/detail?id=8353

Is it possible to find an element like a button and click it, when it is continuously moving on? e.g Carousel

I am using Selenium Webdriver(Ruby) to automate my web app and my web app has this carousel wherein my element continuously keeps moving in a loop.
By the time I locate that element and try to click it, element moves ahead.Hence I am not able to locate that element.
I tried finding and clicking that moving element by following code:
{
ele_button = driver.find_element(:xpath,"xpath")
sleep 10
ele_button.click
}
I thought that by 'sleep 10' I could make that element wait for 10 seconds and then click it.But this does not work and I am getting ElementNotVisibleError whenever I run my script.
Question:
Is it even possible to automate a moving element? If yes please provide me a solution.
Yes it is absolutely possible. I handled same scenario for carousel on my site. There are three ways:
Most carousel stop on mouse hover. So you may use it to stop the
carousel. Use Actions class to move over to the carousel. Once it
stops you may click on it.
If you want a specific slide, you can click on dots or any other navigator, like prev/nxt, to reach your slide and then click it.
The sure shot way to click your specific slide, without worrying about whether it is displayed or not is to use Javascript to click it (Which I had done in my case although I had also implemented 2nd way but I found javascript the simplest solution).
Why are the actions divided?
I would recommend the following:
driver.find_element(:xpath,"xpath").click()
so it will find the object and click on it.
Another thing you can do, as we doing in selenium with java that put the implicitlyWait according to the time on which your button came back after one rotation; now perform click just after implicitlyWait
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
// Suppose a rotation of button takes 30 sec
driver.findElement(By.xpath("/html/body/div[2]")).click();
// action performs on the element
In ruby you have to use this type of syntax
#driver.manage.timeouts.implicit_wait = 30

how to scroll with selenium

I have here a challenge that I spent some time addressing.
Selenium tells me that it cannot click a link that is not visible, so that means I need to scroll my canvas? I am using ubuntu 10, firefox 3, selenium 0.1, ruby 1.9.2, and selenium-webdriver 2.5.0
My code is
driver = Selenium::WebDriver.for :firefox;
driver.get login_url
wait = Selenium::WebDriver::Wait.new(:timeout => 2)
wait.until {
driver.find_element(:name => 'j_password')
}
driver.find_element(:name => 'j_username').send_keys(username)
driver.focus(:name => 'j_username')`
and it says that focus() is not defined. How should I modify my code to put the input element on screen?
Um, this is a hypothetical example, I really need to scroll so that some other element is on the screen, but for simplicity I would like to be able to scroll s.t. any element is on the screen, even
location_once_scrolled_into_view to scroll using ruby.
As per your question your element is not visible, so selenium web driver is unable to click on it.
Simple solution to this is:
Store the xpath of the element which is visible and nearby to your element.
Scroll till that visible element, to make your element visible.
Ensure your element is now visible, click on it
eg code:
element = dirver.find_element(:xpath, "xpath of nearby visible element")
element.location_once_scrolled_into_view
my_element = driver.find_element(:xpath, "xpath of your element")
my_element.click
I had an element at the bottom of the page with no other element close enough to select first. Finally got around it by tabbing from the last field in the form on the page.
element.send_keys(:tab)
When the documentation says "not visible" it's not refering the view port, but to the status of the element on the page. If something is hidden with CSS it cannot be clicked.
If you want to scroll on the firefox window using selenium webdriver, one of the way is to use javaScript in the java code, The javeScript code to scroll down is as follows:
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("window.scrollTo(0,Math.max(document.documentElement.scrollHeight," +
"document.body.scrollHeight,document.documentElement.clientHeight));");
I'm not aware of ruby but the above code can be used as a java Script which will scroll down the whole page.You can even define the area you want to scroll in the view window by simply hard- coding the window.scrollTo(200,350);

Testing if a new window opens with Watir-Webdriver

I'm using Watir-webdriver and I was wondering if there was a good way to check if a new window opens. I've googled around a bit and couldn't find anything though it feels like there should be an easy answer.
I have a printer friendly link and I want to test that the link opens in a new window or tab and I would like to test this with ie, firefox, chrome and safari if possible.
Thanks!
You can check the number of windows:
browser.windows.size
or check if a specific window exists:
browser.window(:title => "foo").exists?
More examples in the specs.
You can also use index based browser window checking where you need to worry about index only and it follows zero based index ordering. So, the default window is of index: 0 and if a new window opens it will be of index: 1, the next will be of index: 2 and so on.
To check first child window if you want to test that the link opens in a new window ,
browser.window(index: 1).exists?
Or to work inside this window,
browser.window(index: 1).use do
# do scripting here
end

Resources