Is there a way to assert that an element (e.g. input or link) has the keyboard focus? I'm using Codeception with Selenium. Couldn't find anything on http://codeception.com/docs/modules/WebDriver
A reliable source told me this works:
$I->executeJS('return $("#element").is(":focus")');
Happy testing!
Some more details to the answer by #sunomad
Activate Codeception's Asserts Module in your acceptance.suite.yml:
modules:
enabled:
# ...
- Asserts
Then use this in an Acceptance test:
$focus = $I->executeJS('return $("#element").is(":focus")');
$I->assertEquals(true, $focus);
Works (through Selenium) with Firefox and Chrome. However, it does not work with PhantomJS - see this issue: https://github.com/ariya/phantomjs/issues/10427
To complete the #sunomad's answer.
If you use the code line :
$I->executeJS('return $("#element").is(":focus")');
You got this error :
[Facebook\WebDriver\Exception\UnknownServerException] unknown error: $ is not a function
Switch $ by jQuery
$I->executeJS('return jQuery("#element").is(":focus")');
And it works !
Related
After Chrome and driver update to 91th version I found the VBA macro with driver 91.0.4472.19 (win32) become return empty string ("") from input tag 'value' attribute:
Dim ch As SeleniumWrapper.WebDriver
Dim el As WebElement
' some code here ...
ch.findElementById("htmlLoginId").SendKeys login
Set el = ch.findElementById("htmlLoginId")
txt = el.getAttribute("value")
With older driver version 90.0.4430.24 this works fine. Hope it will be fixed in next driver versions.
Is there another way to get value from input[type=text] tag? I want to avoid situation if this bug will not be fixed in 92th version and 90th driver version become incompatible.
We have been fighting with this issue a couple of days since the new chrome driver came out. A workaround we did was to bypass calls to the webdriver's getAttribute method if we want to retrieve properties (that are not html attributes) of the element and instead use the following custom script:
browser.executeScript(`return arguments[0].${this.textAttribute}`, el);
The above example is valid for protractor, which is what we are using. If your stack is different, you need to call the appropriate alternative to browser.executeScript, but the script itself would be quite the same.
We did it!
ARG CHROME_VERSION="90.0.4430.212-1"
RUN wget --no-verbose -O /tmp/chrome.deb https://dl.google.com/linux/chrome/deb/pool/main/g/google-chrome-stable/google-chrome-stable_${CHROME_VERSION}_amd64.deb \
&& apt install -y /tmp/chrome.deb \
&& rm /tmp/chrome.deb
./webdriver-manager update --versions.chrome 90.0.4430.24
This was a regression in chrome driver 91. A fix has been made. Hopefully an updated v91 chrome driver will be released soon.
https://bugs.chromium.org/p/chromium/issues/detail?id=1205107
In the meantime we are using chrome driver 90.
I am reproducing this doing getAttribute('innerHTML') as well. As another workaround, run tests with Firefox instead of Chrome
We are also facing the similar issue while getting attribute data of an element using GetAttribute() of web element in C#. Struggled two days to find out what the root cause is. And for us it is not possible to make workarounds as code base is already frozen. Waiting for the fix. And as per the issue https://bugs.chromium.org/p/chromium/issues/detail?id=1205107 it may take couple of weeks to release another version with fix.
The issue has been fixed with the new release of ChromeDriver 91.0.4472.101 and ChromeDriver 92.0.4515.43
During the test, a file (.html) will be downloaded from the web application & I have to verify that file by opening it on the browser. In the non-headless mode, my test is working fine. But whenever I'm going to headless mode, that file is not getting downloaded to the download path (i.e. pointed in the "user.dir"). My chrome driver version is 2.44.609538 & selenium version is 3.14.
Apparently this could help you
Shawn Button post the answer related with it.
Downloading with chrome headless and selenium
Are you running the test from the command line?
Because, according to an answer to this question and this, when you run it via command line, your user.dir corresponds to your global user directory (C:\users\username).
This worked for our ruby implementation:
Capybara.register_driver :scrapping_driver do |app|
options = Selenium::WebDriver::Chrome::Options.new
options.add_argument('--headless')
options.add_argument('--disable-popup-blocking')
options.add_argument('--disable-gpu')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
options.add_argument('--window-size=1366,2000')
options.add_preference(:download, directory_upgrade: true,
prompt_for_download: false,
default_directory: "#{Rails.root}/")
options.add_preference(:browser, set_download_behavior: { behavior: 'allow' })
Selenium::WebDriver::Service.driver_path = Webdrivers::Chromedriver.driver_path
driver = Capybara::Selenium::Driver.new(app, browser: :chrome, options: options)
end
Pay attention at download behaviour
I met same situation.
Headless mode is very faster. So your code might be implemented to detect download(DL).
Solution.
(Current your process?) .click for DL -> Detecting download file generation.
(My solution) Detecting download file generation -> .click for DL.
I implemented the above mechanism using callback function.
I am currently using Nightwatch to test a site running on an MVC controller. All other Nightwatch commands and assertions are working, but when I try to use Expect assertions I get an error:
.expect.element(...).to.be.present is not a function
The element is definitely on the dom and I have the
I have tried requiring chai-nightwatch in my globals file generally and by specifically assigning it to expect, but they continue to error out when I run the test.
client.expect.element().to.be.present WORKS
Recently, we've started to get these kind of warnings on the console when running Protractor tests:
[12252:14584:1207/223118.187:ERROR:process_metrics.cc(105)] NOT IMPLEMENTED
[12252:14584:1207/223118.187:ERROR:process_metrics.cc(105)] NOT IMPLEMENTED
[12252:14584:1207/223318.188:ERROR:process_metrics.cc(105)] NOT IMPLEMENTED
It feels like they happen randomly but doesn't affect the test execution.
The only problem is that they pollute the output console making it more difficult to keep track of tests being executed and test results reported by jasmine/protractor.
Is there a way to turn off this kind of chromedriver warnings?
Using Protractor 5.2.2, ChromeDriver 2.34.
We've found this --silent flag that can be passed to chromedriver executable, but could not find a way to configure protractor to pass this flag when launching chromedriver..
It seems to be an issue with chrome v63
https://github.com/SeleniumHQ/selenium/issues/5189#issuecomment-351605839
You should be able to pass the --silent flag to chromedriver in your conf file. Something like:
capabilities: {
browserName' : 'chrome',
'chromeOptions' : {
args: ['--silent']
}
}
}
"This warning message was generated by a bug in Chrome v63.
Upgrading to v64 (64.0.3282.167 as of this morning) resolves it."
everyone, could I have some help please ?
I try to run a test with Behat/Mink and Phantomjs. It works locally without Phantomjs, but not on my server.
When I launch my tests, I've got this problem :
#javascript
Scenario: Searching for a page that does NOT exist
Given I am on "http://fr.wikipedia.org/wiki/Wikip%C3%A9dia:Accueil_principal"
When I fill in "searchInput" with "Glory Driven Development"
Form field with id|name|label|value "searchInput" not found.
My settings in behat.yml is as following
phantomjs:
context:
class: 'FeatureContext'
extensions:
Behat\MinkExtension\Extension:
goutte: ~
selenium2:
wd_host: "http://localhost:8643/wd/hub"
capabilities:
proxy:
proxyType: direct
My test is as following
#javascript
Scenario: Searching for a page that does NOT exist
Given I am on "http://fr.wikipedia.org/wiki/WikipC3%A9dia:Accueil_principal"
When I fill in "searchInput" with "Glory Driven Development"
And I press "searchButton"
Then I should see "Search results"
I can't understand why it says : "Form field with id|name|label|value "searchInput" not found". I have launched phantomjs and even selenium.
Does someone has an idea ? Thank you
I was trying same scenario today.
Driver is complaining about not finding the element. So I debug a bit the test case and found that the page browsed by driver was different than the one expected.
When the driver does: $this->getSession()->getPage(). Be sure you are in the expected page and receiving expected html.