Selenium WebDriver: is it possible to test WebExtension that inject code to the current page? - selenium

I found articles about opening browser extension as a page - but my extension inject JS to the current page - and extension can not inject code to chrome* pages. My only choice is SikuliX? Also with SikuliX I can test the badge of my button. I think that with SikuliX I simulate real user behaviour - such tests about UI interactions looks like more robust for me. Also nice to test CSS correctness.
I tried to setup a hotkey for my extension:
But
driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + 'I') (Python) or driver.findElement(By.tagName("body")).sendKeys(Keys.CONTROL + "I") (Java)
do nothing, but I can press Ctrl+I and I see popup-UI opened.
UPDATE: I tried to use pyautogui for mouse clicking - but even with opened extension popup UI driver.window_handles does not include it :(

You can configure webdriver to load your extension in to the browser while launching as well. Once your extension in loaded , it can inject required code that i am assuming makes some changes to html such as adding / removing some html elements or applying some styles which can be then tested using Selenium. You can also execute javascript using selenium.
Sikuli framework is based on image recognition and then simulating user interactions on it using mouse and keyboard . Your test cases might break under these scenarios :
Change in resolution which may even result in layout changes
Change in theme of the application resulting in color changes of ui-elements
It will require focus ,etc.

Currently, I test my extension by comparing expected and actual screenshots, using Selenium web driver, pyautogui (for interactions with extension) and opencv2 (for computer vision), see more at https://www.pyimagesearch.com/2014/09/15/python-compare-two-images/

Related

Firefox - hide browser frame with Selenium

I'm trying to figure out how to hide the border (including the address bar, tabs, title bar... everything that isn't the browser viewport) of my Firefox instance instantiated by Selenium.
If there's some way to have it use a userChrome.css, that would be straightforward enough. I've tried loading a profile folder that included a userChrome.css using this answer as a guide, but it seemed to ignore the styles. I've also looked through Firefox's about:config to see if there's some preference that would hide the frame of the window, but I haven't found anything yet.
Any solution that allows me to hide all or some of these elements when creating the instance with Selenium would be helpful. I know it's silly, but that's how it goes sometimes, you know?
-edit-
I don't think the title bar needs to be hidden. But everything else should be hidden.
-another edit to clarify a few things-
I mentioned kiosk mode in the comments as an example of the sort of thing I'm going for. Kiosk mode isn't exactly what I'm looking for, though. The windows aren't meant to be fullscreen, but they should still lack the elements of a common browser window. Think of it as like an Electron app. Out of the box, Electron lacks an address bar, tabs, etc. That's basically what we have for our app, but it's with regular-old Firefox. Again, whether these elements are displayed or not doesn't typically impact the test, but we want them hidden anyway.
Finally, I a friend of mine tried achieving this goal using a userChrome.css wrapped in a Firefox profile and was able to get Selenium to use the userChrome. So perhaps I need to figure out what I'm doing wrong. The biggest difference between how he did it and how I'm doing it is I must use a remote web driver for testing. But even still, it should be able to load the userChrome.css file. I'll try to update this question with more details as I fiddle with it some more.
-edit-
I think the reason userChrome isn't working when specifying a profile is because of the version(s) of Selenium/Geckodriver/Firefox being used.
The geckodriver version I started with was 0.15. 0.17 behaved exactly the same. 0.18 didn't respect the profile I passed along to it at all and instead had Firefox open the profile selection window (not very useful, but I was able to at least select the correct profile and see the userChrome.css get applied). 0.24 is no different.
Firefox is 52.9.0. Not much I can do about that.
We're using selenium (standalone) server 3.8.1. Switching out for 3.141.59 Didn't change anything.
Unless there's a version combination that will work with Firefox 52, I think the only thing I can do is wait until there's an update.
At last I have figured it out. In order to get Selenium to use my custom profile, I needed to do the following:
FirefoxProfile profile = new FirefoxProfile(new File(path_to_profile));
FirefoxOptions options = new FirefoxOptions().setProfile(profile);
RemoteWebDriver driver = new RemoteWebDriver(options.toCapabilities());
driver.get(url_of_webpage);
Thanks to avinesh09 on Github for the info I needed to solve the problem. It's so simple, but this has to be the only way that I neglected to try to load the profile.
If fullscreen (kiosk) mode is what you ask for (as then all you see is the viewport) it is as simple as:
driver.manage().window().fullscreen();
It is the same user experience as pressing "F11" in your browser.

Selenium - Element not visible when the browser set to mobile responsive mode

I am testing the browser for mobile responsiveness. I changed the browser window size to iPhone 5 which is 320 x 568 using this command
driver.Manage().Window.Size = new Size(320, 568);
When I run the test, the browser opens fine according to the mentioned size without any issue. But it fails to find a hyperlink text which is displayed on the page. I get Element not visible exception when I could actually see the link text on the screen. So, could anyone help me solve this issue or have any ideas that I could try?
Any help would be highly appreciated.
Thanks.
Perhaps it's due to the time delay, that means code executes even before the link appears, So write the following code in your language
Code from Ruby Selenium-binding
wait = Selenium::WebDriver::Wait.new(timeout: 10) # seconds
wait.until { driver.find_element(id: "foo").displayed? }
driver.find_element(id: "foo").click
Try to scroll to the element.
You could use java script to do that.
In Python this can be done via
WebDriver.execute_script("arguments[0].scrollIntoView();", elem)
Some elements of the DOM of the webpage change when you test for mobile responsiveness, so selenium is unable to locate the element that you are specifically trying to target.So, you should try to debug and find the methods where the code is failing to perform the action.Then you should find the locators for those elements in "mobile responsiveness view" and trigger only those methods when you are testing for mobile.

Capybara Selenium Navigate To URL Hangs With Popup Alert on Safari

At the end of my tests Capybara automatically navigates to "about:blank" in order to set up the next test. Sometimes the application I'm testing will throw a popup alert if the user leaves the page (which is expected). I have some code to handle this:
begin
page.driver.browser.navigate.to("about:blank")
page.driver.browser.switch_to.alert.accept
rescue Selenium::WebDriver::Error::NoAlertPresentError
# No alert was present. Don't need to do anything
end
This works fine on Firefox, Chrome, and IE. But for some reason on Safari the navigate command hangs, I assume because of the popup. Anyone know a workaround for this?
There is no simple workaround for this at this time in any version of Selenium language bindings. It is a known issue the Selenium team is not interested in resolving. Fundamentally, it is due to the architecture of Safari and consequently the architecture of the Safari Driver.
The JavaScript of the Safari Driver extension does not know about most of the alerts and popups and dialogs that appear as modal Cocoa layer windows.
It also cannot interact with them.
There is a way but it won't be easy and nobody's done it.
You would need to use Cocoa.
So you would want to use RubyCocoa in this case.
(or PyObjC if you used Python)
You would then possibly also want a sidecar app actually written in Objective-C.
The trick would be to use the AX (Accessibility API) and a separate process to observe if there is an alert as the front window and poke at its labels and buttons' text as visible to the AX APIs.
AX APIs are probably exposed in RubyCocoa via the ScriptingBridge.
However, you would need to add your 'app' to the Security preference pane's list of things allowed to control the computer.
With that, you could detect the window and handle it.
It could be fairly brittle across web sites, but if built well, you could handle expected conditions.
You could try to confirm like this which I believe should work across browsers
# click ok to confirm
page.evaluate_script('window.confirm = function() { return true; }')

Auto suggest/complete not loading in webdriver browser instances

I'm having a little trouble nailing down what's causing a particular issue. I'm fairly new to automation testing and I'm having a strange problem. The website I'm testing has an auto suggest function which works absolutely fine when checking manually. The problem is when loading a browser using the Selenium webdriver (I've tried firefox, chrome and IE drivers) that the auto suggest is simply not loading.
It's like the part of the page to do with that and a date/time mini popup aren't loading at all so none of them work when running scripts. Has anyone else had this and resolved it? or is it an issue with the web page itself?
Thanks
You may need to fire an event which by default webdriver is not envoking with sendKeys.
Asking you developer how it works in their code, and then extend selenium to replicate this behaviour.
Also, have you tried do sendKeys one character at a time with a small sleep in-between

How can we test external css file content with selenium UI verification

How to access the external css file data in a Selenium test case for UI verification?
Selenium isn't well-suited for style testing, rather just the DOM. You'd be far better off doing a perceptual difference if checking styles.
There's an open-source utility named pdiff which does this. http://pdiff.sourceforge.net/
There are also commercial tools such as Beyond Compare. http://www.scootersoftware.com/
The idea here is to take a screenshot of the page, which can be done using Selenium, and compare it pixel for pixel against a baseline image.