Nightwatch Cannot Find/Click on Dropdown Option - css

I'm a backpacker and a programmer, trying to use the second skill to find openings in a full campsite. Rather than crawling fro scratch, I'm using the end-to-end testing framework nightwatch.js to navigate for me.
I've hit a roadblock, because nightwatch is having difficulty finding a specific element using css selectors.
Here are the elements and page:
Here is my test code:
Previous Attempts
My test code will click on the selection box with #permitTypeId. It will see that #permitTypeId option is visible. It will not see or click on any of the options when more specific values are specified. The five .click()'s are all css selectors I've already tried. None of the options are set to display:hidden or display:none. I have also tried all of the above without the .waitForElementToBeVisible() just in-case the waiting causes the dropdown to hide.
I've successfully clicked options from different dropdown menus on this website without any problem. Just this one is causing a headache.
The tests are running with the most current Selenium server and Firefox on Mac Yosemite.
tl;dr
Nightwatch.js/Selenium won't click on something from a dropdown menu.

The Path...
Cory got me thinking about jQuery and native DOM manipulation. Tried going that route and was successful selecting the correct option using Selenium's .execute() function:
.execute('document.getElementById("permitTypeId").options[1].selected=true')
However, it was not triggering the onchange event.
Saw this post which made me start thinking about using key-strokes and this one which suggested using arrow-keys to navigate down a <select> element to the option, then hitting enter.
...to the Solution
.click('select[id=permitTypeId]')
.keys(['\uE015', '\uE006'])
I've found that this is an issue with Firefox. Chrome and PhantomJS operate well clicking <option> tags.

you should be able to click like this way
browser.click('select[id="permitTypeId"] option[value="1451140610"]')

Additionally I was able to add a .click event for the specific option once I did a .click for the select. see my example below:
.click('select[name="timezone"]')
.pause(1000)
.click('option[value="America/Chicago"]') //selects the option but doesn't click
.pause(5000)
.keys(['\uE006']) //hits the enter key.

another solution:
.click('select[id="permitTypeId"]')
.waitForElementVisible("option[value='1451140610']")
.click("option[value='1451140610']")

Very simple way is to use .setValue('#element', 'value of option')

Related

Chrome console to click

i would like to use the Chrome-console to click on a button.
So far I always used the Id, like:
$('[id="vcc-eu5"]').click();
or Title
$('[title="Delete this view and its saved rules"]').click();
of the button, but here, I can not find anything like that.
If I inspect the button I can see this:
<a class="wds-button wds-button--primary wds-button--sm apply-btn ">APPLY</a>
Is there a way trigger this button somehow? Where can I read about it?
The currently selected element in the Chrome DevTools is always available as $0.
So, you can just select it and the do $($0).click()
Or you can use a unique combination of its classes:
$('a.wds-button.wds-button--primary.apply-btn').click()

How do I locate elements in protractor that are in other views and are not visible when viewing page source

I am new to Angular & Protractor (and web development for that matter), so I apologize of this is an obvious question.
I am trying to test our angular app with protractor, and it appears that I can locate the first element on the page. But cannot find any of the other elements using (id, name, model, css). I have tried chaining off of the first element, but always get the element not found error on the second element in the chain. I've have triple check the spelling so I am confident everything is correct.
Our page is setup up with multiple sections and when I "view source" I only see the root div.
<head>
</head>
<body>
<div ng-app="app" id="wrap">
<div ui-view></div>
</div>
</body>
</html>
But when I inspect the elements using the developer tools (F12), they exist in the DOM, I just don't know how to get to them.
<input type="text" class="form-control ng-valid ng-dirty ng-valid-parse ng-touched" data-ng-model="vm.searchText" id="searchText" placeholder="(Account # / Name / Nickname / Phone #)">
I tried to access the control listed above using the following:
browser.element(by.id("searchText").sendKeys("test");
browser.element(by.model("vm.searchText").sendKeys("test");
element(by.id("searchText").sendKeys("test");
element(by.model("vm.searchText").sendKeys("test");
I also create a single button and used partialButtonText & buttonText, neither of which worked.
I also tried to add some async functionality with "then" but that didn't work either. How do I access these elements are are not contained in a single html file?
thanks.....
If an element is not visible, I believe protractor isnt able to interact with it. It can't click or get text or anything if it is not visible, that is actually checked before it can perform the action.
What you can do is check the element is present to ensure it is somewhere on the html.
var searchText = $('#searchText');
expect(searchText.isPresent()).toBeTruthy('Search Text element not present');
This will find an element with a css selector of id searchText, and then check if it is present(exists on the html).
If you want to interact with it, remember that protractor looks around like a human would. If a human cant click it, neither can protractor! Make sure it is on the page and visible.
Don't have the reputation points to add this in the comments to user2020347's response so...When you say not in "view source" I assume you're talking about dynamically generated content. Instead of using view source either use chrome or firefox developer tools to make sure you're using the right locators.
For example in chrome's console the following should return a result once the page is loaded:
$$('#searchText')
$$('input[data-ng-model="vm.searchText"]')
It also looks like you're sending keys to the same element.
Since you have an angular app protractor should wait for all elements to load, but just in case you might want to wait for the element to be present and/or visible on the page.
Same happened to me, because Protractor executed on the original (first) tab.
Try to switch between the tabs before accessing the elements:
browser.getAllWindowHandles().then(function (handles) {
browser.driver.switchTo().window(handles[1]);
});

How to click on Toolbar element with Sahi for Test Automation?

Web page contain a button with some text for example "Test". This button actually is a toolbar element. (class ="tbButton" id="id",text="Test") and redirects to a certain table when press on it.
When I tried to use the following click methods, the button did not react.
browser.div("Test").click();
browser.click("id");
browser.click("");
browser.div("id").click();
browser.byId("id").click();
browser.containsText(browser.byId("id"),"Test");
browser.div("Test").in(browser.table("Generar")).click();
browser.byXPath("//div/Test").click();
Could anybody suggest me an alternative methods that is able to resolve the above problem?
Try with:
browser.xy(browser.div("Test"), 10, 10).click();
This will click a little inside the div.

WebDriver HtmlUnitDriver NoSuchElementException

I'm using Webdriver to test my web application. When I work with FireFoxDriver or ChromeDriver everything seems to be ok. When I work with HtmlUnitDriver though things start to go wrong.
Here is a sample code:
WebDriver driver = new HtmlUnitDriver();
driver.get("http://localhost:8099/");
WebElement loginButton = driver.findElement(By.xpath("//button[#type='button']"));
loginButton.click();
i'v looked at the driver.getPageSource result, and the source code presented there is very partial.
it doesnt show me all the elements. it is the same a clicking view source on the page.
what i need from the driver is the entire source, like firebug or chrome inspector give me.
any ideas on how i can retrieve it?
my app was written with the GWT.
thanks a million
Have you tried to enable JavaScript for HtmlUnitDriver?
I believe that the HTMLUnitDriver emulates IE by default (link) and there are other questions related to clicking buttons with IE. Have you tried this?
// Press enter on the button
loginButton.sendKeys("\n");
Also, have you tried adding an ID to the element and using that to find the button?

How to use seleniumRC in Junit framework on dynamically changing elementids

I am trying to automate a work flow process .In this,I need to click on a link positioned in any of the rows of table.Thing is all links available in all rows have same element ID and in the source code I have a java script like " ("Element ID" # Onclick..java script****:).....SO here after clicking it is connecting one form to another form by inputting some value in java script code and also one value in java script dynamically changes.How do I click on that link now?Is there any solution using xpath or so...to exactly click on that link based on CSS classID or so...Please help me out..Main problem is...all links in rows have same element ID and dynamically changing java script .
I am trying to use selenium.focus() and selenium.clickAndwait().But these are helpless.as it is not able to identify link ID only.
The best way to do this would be with xpath.
Something like //*[#onclick='javascript'] will work but this can make the tests extremely flaky because if the inline javascript changes or if its removed in preference of addEventListener to the element.
something like //*[#class='cssClass'] will work. I think that you will need to speak to the developers and ask them to help make it more testable.

Resources