Looking for alternative to sleep(30) in watir - watir

I am trying to apply sleep on page loading that works completely fine, but I want to avoid using it and looking for an alternative.
I have tried these options:
#browser.element(:class => 'oo-pricing').wait_until(:timeout => 30)
Watir::Wait #browser.span(:class => 'oo-blp-price').wait_until(:timeout => 30)
My goal is to wait until the text in span is updated.
browser.goto('https://secure.bestprice.rankingsandreviews.com/nc/configurator/307715')
browser.element(:id => 's2id_select_style').click
browser.element(xpath: ".//*[#id='select2-drop']/div/input").send_keys('Convertible LT 1LT', :tab)
browser.element(id: 'edit_color').click
browser.span(text: 'Incentives').click
browser.element(text: "2017 GM Conquest Cash").parent.preceding_sibling.span.click

You are using Chrome driver, Chrome driver automatically closes the browser. If you had used Firefox driver, you would not be having this problem, The solution to your problem is,
Add this code
require 'watir'
caps = Selenium::WebDriver::Remote::Capabilities.chrome(chrome_options: {detach: true})
browser = Watir::Browser.new :chrome, desired_capabilities: caps
And then write your code
browser.element(:id => 's2id_select_style').click
browser.element(xpath: ".//*[#id='select2-drop']/div/input").send_keys('Convertible LT 1LT', :tab)
browser.element(id: 'edit_color').click
browser.span(text: 'Incentives').click
browser.element(text: "2017 GM Conquest Cash").parent.preceding_sibling.span.click
browser.span(text: 'Save & Update').click #Updated
Now your chrome browser would not closing after the program ends.

Find the element that you want, for example:
#browser.element(:class => 'oo-pricing')
Call the wait_until_present method on it:
#browser.element(:class => 'oo-pricing').wait_until_present

Related

Problems with Richefaces default editor on IE9 or edge

I had a problem with default editor (TinyMCE) of Richfaces version 3.3.1.GA when the user is using Internet Explorer 9. I've read many people here and on the Internet who prefer to use X-UA-Compatible metadata to downgrade IE version, but, I dislike it. I always prefer to use the edge version because I'm using CSS 3 as well.
Another solution was a upgrade of RichFaces however I've not considered the possibility for the sake that my application is a pretty legacy.
Then I started unzipping the RichFacesUI jar and changing the file META-INF/resources-config.xml from:
<resource>
<name>scripts/tiny_mce/tiny_mce.js</name>
<path>org/richfaces/renderkit/html/scripts/tiny_mce/tiny_mce.js</path>
<renderer class="org.ajax4jsf.resource.ScriptRenderer"/>
</resource>
to:
<resource>
<name>scripts/tiny_mce/tiny_mce.js</name>
<path>org/richfaces/renderkit/html/scripts/tiny_mce/tiny_mce_src.js</path>
<renderer class="org.ajax4jsf.resource.ScriptRenderer"/>
</resource>
This is to ease the script debugging. That would be pretty hard without it.
First of all these problems are occur because Richfaces uses a specific statement to old IEs therefore I putted the following code to know what version of IE is execution:
var ie = (function(){
var undef,
v = 3,
div = document.createElement('div'),
all = div.getElementsByTagName('i');
while (
div.innerHTML = '<!--[if gt IE ' + (++v) + ']><i></i><![endif]-->',
all[0]
);
return v > 4 ? v : undef;
}());
PS: This clever code is from James Padolsey and I found it here in this question.
Then I ran my application and where it broke I changed the closest IE if statement as the following examples:
Before it was:
if (isIE) {
Then it became:
if (ie < 9) {
And when it was negative statement like:
if (!isIE) {
Then it became:
if (!isIE || ie > 8) {
The only difference was in the getXML function which was like:
if (!i || !i.createDocument) {
Then I changed to:
if (!i || !i.createDocument || ie > 8) {
In this Gist are all changes that I did. I hope that it helps anyone with this problem.
If you want I putted the jars which I changed on the following links:
Richfaces-impl-3.3.1.GA
Richfaces-ui-3.3.1.GA
Please, sorry my English mistakes, you are encouraged to re-write and fix this post.

Perl gtk2 window update

Recently when i was writing gtk2 program in perl I've runned into a little problem. I've got a main window which uses variables in labels and buttons to display user some data.
my $label1 = Gtk2::Label->new ("IP ".$target_ip);
my $label2 = Gtk2::Label->new ("Port ".$target_port);
my $label3 = Gtk2::Label->new ("Threads ".$thread_number);
And after updating $target_ip variable (by pop up window) or any other variable used in my main window for that matter by user nothing happens, so my question is how can i update window in Gtk2 i've tried re drawing it by calling window function again but for some reasons the old one still stays up even after issuing Gtk2->main_quit. Also i'm aware this may be a lame question but i'm using Gtk2 library for the first time to write a major project and I'm not really expert in it yet.
Here's my code so far ($ok variable represents ok button in pop up window which updates other variables in main window)
$ok->signal_connect (clicked => sub {
$target_ip = $text_area->get_text;
Gtk2->main_quit; #Pop-up window
main_Gtk()
#Rebuild Attack window with given parameters / variables
#!/usr/bin/perl
use strict;
use warnings;
use Glib qw/TRUE FALSE/;
use Gtk2 '-init';
my $window = Gtk2::Window->new('toplevel');
$window->signal_connect('delete_event' => sub { Gtk2->main_quit; });
$window->set_default_size(300, 200);
my $target_ip = '127.0.0.1';
my $label1 = Gtk2::Label->new ("IP ".$target_ip);
my $vbox = Gtk2::VBox->new(FALSE, 3);
$vbox->pack_start($label1, FALSE, FALSE, 4);
my $button = Gtk2::Button->new("Ok");
$vbox->pack_end($button, FALSE, FALSE, 4);
$button->signal_connect(clicked => sub {
$target_ip = '88.88.88.88';
$label1->set_label("IP ".$target_ip); # That's what you need!!!
});
$window->add($vbox);
$window->show_all;
Gtk2->main();

How to change the flash color?

I want to change the flash color when using element.flash method. By default its yellow.
I was able to increase the number of flash times and delay time in elements.rb file. But i dont know how to change the highlight color.
Any idea on this?
Using different colors to highlight will be helpful if browser elements have yellow background.
Solution
The flashing is based on the element's container's activeObjectHighLightColor. This is set by doing:
element.container.activeObjectHighLightColor = "colour"
Where colour is a valid web-friendly color (as per the container.rb file).
Example - Flash For Individual Element
As an example, here is changing the flash colour for the text field on the Google search:
#Use google search text field as a test page
ie = Watir::Browser.new
ie.goto 'www.google.ca'
e = ie.text_field(:name => 'q')
#Set the flash colour
e.container.activeObjectHighLightColor = "green"
#Flash the object, which should now be green
e.flash
Note:
This will only work with Watir-classic. Watir-webdriver does the flashing differently.
I only tested this in the latest version of watir-classic, but the code for 2.0.4 appears to be the same.
Example - Default Flash Colour
To change the default flash colour for everything, you need to set the activeObjectHighLightColor for the browser.
If you want to change it for the current browser, do:
ie = Watir::Browser.new
ie.activeObjectHighLightColor = "green"
ie.goto 'www.google.ca'
e = ie.text_field(:name => 'q')
e.flash
#=> Will flash green
If you want to change it permanently (ie so you do not have to set it each time), you can change the colour in the ie-class.rb file:
HIGHLIGHT_COLOR = 'yellow'

Watir rautomation mouse move does nothing

I'm trying to get a hidden element visible. I've tried every mouse event and element selector I can find in Watir. So I'm trying to use RAutomation code that's already within watir. But it seems ineffective.
require 'watir'
#browser = Watir::IE.new
#browser.goto 'samplesite.com'
#browser.rautomation.mouse.move({:x=>210,:y=240})
From this I get the browser to raise to the top, and the console replies nil. But the mouse hasn't moved. Then when I proceed to do
#browser.rautomation.mouse.click
I get a response of 2... which I take as a success of the click. I need to be able to move the mouse to this coordinate.
#browser.rautomation.adapter
returns => :win_32
Can I set it to :autoit in an already established instance of Watir?
Help please.
So, what you're saying is that the mouse does not move at all?
Try this code:
mouse = #browser.rautomation.mouse
puts mouse.position.inspect
mouse.move :x => 100, :y => 100
puts mouse.position.inspect
What is the output?
To use AutoIt adapter, you can do this:
mouse = RAutomation::Window.new(:adapter => :autoit, :hwnd => #browser.hwnd)
mouse.move :x => 100, :y => 100

How do I use Watir to download a file to a specific location in IE9

In the older versions of IE, there is a popup window.
In IE9 it's a funny looking bar on the bottom.
Anybody had tried anything that works yet?
The only way for now is sending TABs.
For example with Watir and RAutomation
ww = Watir::IE.attach(:url, /1.1.1.127/)
#Click on download link
ww.link(:text, /Download_link/).click
#Click on 'Save As' in the 'funny looking bar on the bottom'
wrauto = RAutomation::Window.new(:title => ww.title)
wrauto.send_keys("{TAB}")
wrauto.send_keys("{TAB}")
wrauto.send_keys("{DOWN}")
wrauto.send_keys("{DOWN}")
wrauto.send_keys("{ENTER}")
#popup window to save
w = RAutomation::Window.new(:title => /Save As/)
#Direction that you going to save
w.text_field(:class => "Edit", :index => 0).set path_direction
w.button(:value => "&Save").click
RAutomation::Window.new(:title => /complete/).button(:value => "Close").click

Resources