Running Chrome's Mobile Emulation with Robot Framework?

3,508 views
Skip to first unread message

nreso...@imvu.com

unread,
Feb 22, 2017, 2:15:29 PM2/22/17
to robotframework-users
I'm trying to test how our website looks on various devices.  My idea was to use Chrome's mobile emulation, as shown here:  https://sites.google.com/a/chromium.org/chromedriver/mobile-emulation

I made a Robot keyword that ran the code shown there, and it successfully starts up the emulator (when paired with a Selenium Standalone Server, of course), but I can't get Robot to actually play in that environment.  Instead it either creates a different window to work in or complains there's no browser open, and I just see an empty Chrome Mobile Emulator window open.

I'm assuming the problem is that the web driver created by the code in the first link isn't the same driver as the one Robot's using.  So how do I fix that problem so that I can run Robot with the Chrome Mobile Emulator?

nreso...@imvu.com

unread,
Feb 22, 2017, 4:54:04 PM2/22/17
to robotframework-users
Just to play with things I've tried so far:

First of all, this is in the Python code:

def switch_to_mobile_emulation():
    mobile_emulation = { "deviceName": "Google Nexus 5" }
    chrome_options = webdriver.ChromeOptions()
    chrome_options.add_experimental_option("mobileEmulation", mobile_emulation)
    driver = webdriver.Remote(command_executor='http://127.0.0.1:4444/wd/hub',
                          desired_capabilities = chrome_options.to_capabilities())


I tried running 

Open Browser  ${Webpage}    chrome  remote_url=http://127.0.0.1:4444/wd/hub  #desired_capabilities=${Capabilities}

with Capabilities being returned from the above code... it opens, but it's not emulating anything.  I've tried returning driver from the above code and running  

Switch Browser  ${Driver}

But it keeps saying that ${Driver} is "None".  

Running Switch To Mobile Emulation just opens an emulation page that's pointed at "data:," but then if I don't open another browser it complains that no browser is open.  

So what else is there to do here?

Tatu Aalto

unread,
Feb 23, 2017, 2:13:13 AM2/23/17
to nreso...@imvu.com, robotframework-users
Ugh

Selenium can only control browser which was started by selenium and this is by design in selenium and not in Selenium2Library. Also in Selenium2Library there is not a keyword which would allow you to register Webdriver instance to the Selenium2Library side in Robot Framework data.

But to overcome your problem, you have two choices. 1) Use Create Webdriver keyword 2) Use custom keyword to start the browser

1) With Create Webdriver keyword it might be possible to give all the required parameters that you need to start the browser. I am not totally sure is that possible but at least it's worth of try.

2) If the first option does not work, then you can create custom keyword to start the browser and register the driver instance to the Selenium2Library side.

First you need to get the Selenium2Library instance from the Robot Framework[1]. Then create the browser as you want. And last register the driver instance to the Selenium2Library instance by using `_cache.register` method. A usage example of the method can be found from Open Browser keyword source [2], see line 134. After the registration you can use the created browser normally from the Selenium2Library.

--
You received this message because you are subscribed to the Google Groups "robotframework-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to robotframework-users+unsub...@googlegroups.com.
To post to this group, send email to robotframework-users@googlegroups.com.
Visit this group at https://groups.google.com/group/robotframework-users.
For more options, visit https://groups.google.com/d/optout.

nreso...@imvu.com

unread,
Feb 23, 2017, 4:51:08 PM2/23/17
to robotframework-users, nreso...@imvu.com
Hey thanks, I never would have thought of that without your help, and it worked.

For those who want to try this, here's the python code that worked for me:

def switch_to_mobile_emulation():
    seleniumlib = BuiltIn().get_library_instance('Selenium2Library')
    mobile_emulation = { "deviceName": "Google Nexus 5" }
    chrome_options = webdriver.ChromeOptions()
    chrome_options.add_experimental_option("mobileEmulation", mobile_emulation)
    driver = webdriver.Remote(command_executor='http://127.0.0.1:4444/wd/hub',
                          desired_capabilities = chrome_options.to_capabilities())
    return seleniumlib._cache.register(driver, "Remote")

That, thrown into a library, gives you a keyword that can be followed with a command to go to a page and you'll get emulation up and running (for the Nexus 5, at the moment).

Omar Fraige

unread,
Apr 18, 2017, 11:43:32 AM4/18/17
to robotframework-users, nreso...@imvu.com
Hi, im trying to open a mobile emulation using robot for a school project but all my codes just start the emulation and instantly close the browser, 
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

def Mobile_Browser_Driver():
mobile_emulation = {
    "deviceMetrics": { "width": 360, "height": 640, "pixelRatio": 3.0 },
    "userAgent": "Chrome (Windows; Android 4.2.1; en-us; Nexus 5 Chrome/18.0.1025.166 Mobile" }
chrome_options = Options()
chrome_options.add_experimental_option("mobileEmulation", mobile_emulation)
driver = webdriver.Chrome(chrome_options = chrome_options)

can u help me pls? 

i trying your code but the Log send me the next error "global name 'BuiltIn' is not defined"

I appreciate your given time and attention

Hélio Guilherme

unread,
Apr 18, 2017, 5:51:44 PM4/18/17
to robotframework-users
@Omar Please remember that this is about RobotFramework. The `Builtin` methods are from robotframework API. In the case was loading the Selenium2Library (a port from original Selenium to RobotFramework).

Can't help you about your specific problem.

My Favorite Open Source Projects
awsome-lists gretl robotframework
(sponsored/patrocinado) Recomendo servidores e alojamento Web em:
http://www.proalojamento.pt/

--

Kashif Shehzad

unread,
Feb 28, 2018, 8:29:13 PM2/28/18
to robotframework-users
Hi,

I need help with this. Can you please tell me if i got the steps correct?
1. create python file and paste your code (name the file as abc.py, for example)
2. create a robot file ( in it import the abc.py file as a Library, under Settings)
3. What should go under the Keywords section?

Tatu Aalto

unread,
Mar 1, 2018, 9:31:14 AM3/1/18
to kashi...@gmail.com, robotframework-users
Ugh

For 1and 2, in really high level, you are correct. For more details about libraries, please refer to the user guide [1].
For 3, well it depends. I tend to belong to the side who says nothing but one can create as many user keyword as one pleases. If you create your library in such way that library keywords can be used in test, then you don't need anything in the keywords table. But one can create user keyword from other keywords and use them in tests.


-Tatu
Send from my mobile

--

WinterSoldier

unread,
Mar 8, 2018, 4:17:52 PM3/8/18
to robotframework-users
Do you have an example test case?

I've tried several methods, but I am not seeing this work.

Thanks,
WS

Styris

unread,
Mar 8, 2018, 5:49:29 PM3/8/18
to robotframework-users
Hi,

I tried this code:
from robot.libraries.BuiltIn import BuiltIn
from selenium.webdriver.remote import webdriver




def switch_to_mobile_emulation():
seleniumlib = BuiltIn().get_library_instance('Selenium2Library')
    mobile_emulation = {"deviceName": "iPhone 8"}
chrome_options = webdriver.

chrome_options.add_experimental_option("mobileEmulation", mobile_emulation)
driver = webdriver.Remote(command_executor='http://127.0.0.1:4444/wd/hub', desired_capabilities=chrome_options.to_capabilities())

return seleniumlib._cache.register(driver, "Remote")

The error i get right away is "AttributeError: 'module' object has no attribute 'ChromeOptions'". 
Please advise. Thank you!
To post to this group, send email to robotframe...@googlegroups.com.

WinterSoldier

unread,
Mar 21, 2018, 1:23:18 PM3/21/18
to robotframework-users
I got mine to work using a slightly different method than what is documented:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

    @keyword
    def open_emulated_mobile_browser(self, device):
        seleniumlib = BuiltIn().get_library_instance('lib')
        mobile_emulation = { "deviceName": device }
        chrome_options = webdriver.ChromeOptions()
        chrome_options.add_experimental_option("mobileEmulation", mobile_emulation)
        driver = webdriver.Chrome(desired_capabilities = chrome_options.to_capabilities())
        return seleniumlib._cache.register(driver, "Remote")

Essentially, I removed "command_executor = 'http://localhost:3128/wd/hub',", and changed webdriver.Remote to webdriver.Chrome.

I am running this locally.
Reply all
Reply to author
Forward
0 new messages