How to wait in watir-webdriver to load next page and then do assert on some random text - watir-webdriver

I am new to watir, and (believe me) I tried all the options that are avaliable here for adding wait and assert but have not been successful in doing so.
Here's a brief description of what I'm trying to do and appreciate your help.
I log into my website
I login using userid/password and hit click button.
I want watir to wait until next page is loaded
I want to do some assert to verify the login was successful.
My step 3 and step 4 are failing with the error element not found.
If I login manually and login is successful, I see text Hello,username at the top of the second page and this is what I've been trying to key off of but not having any success. It could be something very simple, but since I'm new to watir, I'm unable to figure it out.
Here are all the commands I tried:
$b.button(:id, 'usernameLogfaceinButton').click
#$b.wait_until($b.text.include?("Hello"))
#($b.text.include?("Hello")).wait_until_present
#$b.text_field(:text, "Hello").wait_until_present
if $b.text.include? "Hello"
puts "Test Passed. Found the test string: Hello- Actual Results match Expected Results."
else
puts "Test Failed! Could not find: Hello"
end
HTML on Page:
<body id="myAccounts" class="cardholderLayout ">
<div id="content">
<div id="navigationContainer">
<div id="headerNavigationMenu" class="innerContents">
<div class="logo"><img alt="xxx" src="/images/xxx.png?xxxxxxxx"></div>
<ul class="menu">
<li class="menuItem">
Hello, Bob
I hope someone can help me or point me in the right direction.

Try:
$b.link(:text => /^Hello/).wait_until_present
Notice that:
It is $b.link instead of $b.text_field. The method needs to match the type of element you are looking for.
The text in the locator is /^Hello/ instead of "Hello". If you pass a string (ie "Hello", watir will look for an element where the text exactly matches. If you want to do a partial text match, then you need to use a regular expression. The expression /^Hello/ says to find a text starting with "Hello".

Related

Selenium find button inside div by XPath

I need to find "Confirm" and click on.
<div class="sidebar-element-confirm">
<ui-button class="btn btn-xm sidebar-element-confirm" type="button">
Confirm
</ui-button>
</div>
My solutions are (neither of them works -- no exception, no error):
private val element1 = "//div[#class='sidebar-element-confirm']/ui-button[contains(text(),'Confirm')]"
private val element2 = "//div[#class='sidebar-element-confirm']/ui-button[#type='button']"
driver.findElement(By.xpath(element1)) --does not work
driver.findElement(By.xpath(element2)) --does not work
Could you please tell me what is wrong with my XPath?
Updated #1:
Chrome (XPath) gives me:
//*[#id="right-sidebar"]/div/div[2]/ui-button
However, this XPath did not work:
val chrome_xpath = "//*[#id='right-sidebar']/div/div[2]/ui-button"
webDriver.get(URL)
searchId.sendKeys(Id) --put id of order
searchId.sendKeys(Keys.ENTER) -- ENTER
Thread.sleep(1000)
... the order is loading, then I need to click on "Confirm".
assert(webDriver.findElement(By.xpath(chrome_xpath).isInstanceOf[WebElement]) # true
webDriver.findElement(By.xpath(chrome_xpath)).click`
Button was not touched. Exception:
StaleElementReferenceExpection:Element is no longer valid. Thrown at a line when I tried to click the button
Updated #2:
private val confirmButton = ".btn.btn-sm.btn-block"
webdriver.get(URL)
login --and Spinning
ExplicitWait until some element is found on the page
findElement -- put there an Order ID and press Enter --and Spinning
ExplicitWait until Order is appeared on the page
findElement(By.cssSelector(confirmButton)).click -- passed only once! --and Spinning
ExplicitWait
The rest of mine attempts were failed either by "IE driver has stopped" or "StaleElementException" or "Element is no longer exists"
Would you please tell me if i do smth wrong in designing that test?
Use this following XPath:
1. //*[normalize-space()='Confirm']
2. //div[normalize-space()='Confirm']
3. //div//ui-button[normalize-space()='Confirm']
normalize-space(): Remove the spaces before and after a string that placed in between HTML tags.

"Clicking" on a not visible element

So my current situation is that I am trying to click on a marker based in a Google Maps window on a webpage. I have successfully located the markers in two manners: element.all(by.css('.angular-google-map-marker')) and element.all(by.repeater('m in map.markers')).
I have proven that I am obtaining the correct elements by changing the location on the Google Map and using count() to retrieve the number of markers present which returns the correct number in every case.
However, when I try to do for example element.all(by.css('.angular-google-map-marker')).first().click(), I receive the following error:
Failed: element not visible
HTML section
<div ng-transclude="" style="display: none">
<span class="angular-google-map-marker" ng-transclude="" ng-repeat="m in map.markers" options="m.options" coords="m.coords" idkey="m.id" click="onMarkerClick"></span>
<span class="angular-google-maps-window" ng-transclude="" coords="activeMarker.coords" options="windowMapOptions" show="windowMapOptions.show" closeclick="closeInfoWindow" templateurl="'gMapInfoWindow.html'" templateparameter="activeMarker"></span>
</div>
Normally elements that trigger some event due to clicking have an attribute like ng-click= foo(), however the markers above only use click= foo(). In addition if you look the line with the div tag, it says display: none, which might explain the visibility error.
My Question: Is there a way to activate the effect of an attribute like click= foo() without clicking on the element directly?
Aside from trying to make an element visible and then clicking, you can attempt clicking "via JavaScript" (there are some differences though - WebDriver click() vs JavaScript click()):
var marker = $('.angular-google-map-marker');
browser.executeScript("arguments[0].click();", marker.getWebElement());
First of all be sure that you can interact with the element when it is visible, you can do this from within the DevTools of your browser, make it visible by adding a display:block. Then check that if you change the value of the selectbox, the value can also be used with Angular Binding.
If so, you can easily make the element visible with Protractor by injecting a piece of Javascript in the page with the following command:
browser.executeScript('document.querySelector("div").style.display = "block"');
This results in a promise, so be aware of that!

Protractor can't find element by binding

I'm trying to find a element by binding, the problem is that the the element is an toast.
I'm using:
element(by.css('.btn-primary3')).click()
To simulate the click. As a result the toast does appear in the browser during the test.
Then I'm trying to store the element in a variable and test if the text value of the toast is equal to the expected value.
var toast = element(by.binding('toast.toast.title'));
expect(toast.getText()).toEqual('Inloggen mislukt');
But here the error pops up.
Failed: No element found using locator: by.binding("toast.toast.tile")
When I check the toast element in the the Chrome dev tools it shows up like this,
<div data-ng-repeat="toast in activeToasts">
<span data-ng-bind="toast.toast.title" class="ng-binding"> Inloggen mislukt</span>
</div>
I think the problem comes from the fact that that the span containing the binding doesn't exist on the dom when the page is loaded. It gets created when the button is clicked.
If this is the case, wait for the presence of the element after clicking the button:
element(by.css('.btn-primary3')).click();
var toast = element(by.binding('toast.toast.title'));
browser.wait(EC.presenceOf(toast), 5000);
expect(toast.getText()).toEqual('Inloggen mislukt');
May you should try:
<div data-ng-repeat="toast in activeToasts">
<span data-ng-bind="toast.toast.title" class="ng-binding"> Inloggen mislukt</span>
</div>

Attempting to verify disabled text with Selenium IDE

I am attempting to verifyText of a text box programmatically populated then is disabled from user editing. Firebug gets me to this code.
<div class="flexitem-center">
<input class="input-default" type="text" disabled="" value="20140201"/>
I can locate the text box but I can't figure out how to verify against the value attribute. I am fairly new to Selenium and only use the IDE not web driver.
I have found guru99.com "How to use Locators in Selenium IDE" to be real helpful, but I could not find and answer for this. The same goes with seleniumhq.org/docs/
I haven't found any examples of verifying the value attribute. Maybe I can't get there from here?
My most recent attempt is
<td>verifyText</td>
<td>css=input.input-default[value=20140201]</td>
<td>20140201</td>
That generates an error - Actual value " did not match '20140201'
Thanks for any help.
First of all if you can find an element by locator = css=input.input-default[value=20140201] you are already sure that the input with such value exists (because it is included to locator). So you can do:
click | css=input.input-default[value=20140201]
And it will fail if there will be no element with such value
Secondly value is not the same thing as text. Value is an attribute of input. If you need to verify attribute value just do it:
verifyAttribute | css=input.input-default[value=20140201]#value | 20140201

Using Watir with Chrome browser to find element on just opened web page

I try to find button on just opened page without sleep command.
HTML:
<span id="create-user-button" class="ui-button ui-widget ui-state-default
ui-corner-all ui-button-text-only" role="button" aria-disabled="false">
<span class="ui-button-text">Create‌·User</span>
</span>
Initialization of my element:
span(:create_user_button, :id => "create-user-button")
def open_user_creation_dialog
sleep 1
self.create_user_button_element.when_visible.click
end
When i use piece of code without sleep for Chrome browser, my page is closed and i can't find defined button. There isn't such problem for FF.
I try to use a lot of variants to fix it, like :
self.create_user_button_element.when_visible(2).click
self.create_user_button_element.when_present(2).click
Solution was found :
My page doesn't use Ajax, but there is very big table with users on my page, it takes a lot of time to download.
So i am waiting for download big table and after that i find very well my element.
My problem was solved.
div(:total_users_number, :id => "users-table_info")
def open_user_creation_dialog
self.wait_until(10, "No users displayed") do
self.total_users_number_element.when_visible.text != ""
end
self.create_user_button_element.when_visible.click
end
I have page with a very loadful content, namely list of users, so i should wait for element with big content. I do it, this way :
div(:total_users_number, :id => "users-table_info")
self.wait_until(10, "No users displayed") do
self.total_users_number_element.when_visible.text != ""
end
When this element loaded, i can find my sought element
self.create_user_button_element.when_visible.click