In its default mode, WebDriver will launch a browser and run the test scripts in the browser, then exit. If you like to work exclusively from the command line, then you need to look into running the browser in headless mode. Fortunately, this is easy to do with Firefox on Ubuntu. Here’s what you need to do:
Install the official Firefox Beta PPA:
$ sudo apt-add-repository ppa:mozillateam/firefox-next
(this will add the file /etc/apt/sources.list.d/mozillateam-firefox-next-trusty.list and also fetch the PPA’s key, which enables your Ubuntu system to verify that the packages in the PPA have not been interfered with since they were built)
Run apt-get update:
(this will add the file /etc/apt/sources.list.d/mozillateam-firefox-next-trusty.list and also fetch the PPA’s key, which enables your Ubuntu system to verify that the packages in the PPA have not been interfered with since they were built)
Run apt-get update:
$ sudo apt-get update
Install firefox and xvfb (the X windows virtual framebuffer) packages:
Install firefox and xvfb (the X windows virtual framebuffer) packages:
$ sudo apt-get install firefox xvfb
Run Xvfb in the background and specify a display number (10 in my example):
Run Xvfb in the background and specify a display number (10 in my example):
$ Xvfb :10 -ac &
Set the DISPLAY variable to the number you chose:
Set the DISPLAY variable to the number you chose:
$ export DISPLAY=:10
Test that you can run firefox in the foreground with no errors:
Test that you can run firefox in the foreground with no errors:
$ firefox
(kill it with Ctrl-C)
Now run your regular Selenium WebDriver scripts (no modifications required if they already use Firefox as their browser).
Here is an example of a script I have written in Python, which clicks on a category link in an e-commerce store, adds an item to the cart, that starts filling out the user’s information in the cart:
(kill it with Ctrl-C)
Now run your regular Selenium WebDriver scripts (no modifications required if they already use Firefox as their browser).
Here is an example of a script I have written in Python, which clicks on a category link in an e-commerce store, adds an item to the cart, that starts filling out the user’s information in the cart:
# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
import unittest, time, re, random
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
import unittest, time, re, random
class SelWebdriverNewUser(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
self.driver.implicitly_wait(20)
self.base_url = “http://myhost.mycompany.com/"
self.verificationErrors = []
self.accept_next_alert = True
def setUp(self):
self.driver = webdriver.Firefox()
self.driver.implicitly_wait(20)
self.base_url = “http://myhost.mycompany.com/"
self.verificationErrors = []
self.accept_next_alert = True
def test_sel_webdriver_new_user(self):
driver = self.driver
HOST = “myhost.mycompany.com”
RANDINT = random.random()*10000
driver.get(“https://” + HOST)
# Click on category link
driver.find_element_by_xpath(“//*[@id=’nav’]/ol/li[3]/a”).click()
# Click on sub-category link
driver.find_element_by_xpath(“//*[@id=’top’]/body/div/div[2]/div[2]/div/div[2]/ul/li[4]/a/span”).click()
# Click on product image
driver.find_element_by_xpath(“//*[@id=’product-collection-image-374']”).click()
# Click Checkout button
driver.find_element_by_xpath(“//*[@id=’checkout-button’]/span/span”).click()
driver.find_element_by_id(“billing:firstname”).clear()
driver.find_element_by_id(“billing:firstname”).send_keys(“selenium”, RANDINT, “_fname”)
driver.find_element_by_id(“billing:lastname”).clear()
driver.find_element_by_id(“billing:lastname”).send_keys(“selenium”, RANDINT, “_lname”)
# Click Place Order
driver.find_element_by_xpath(“//*[@id=’order_submit_button’]”).click()
driver = self.driver
HOST = “myhost.mycompany.com”
RANDINT = random.random()*10000
driver.get(“https://” + HOST)
# Click on category link
driver.find_element_by_xpath(“//*[@id=’nav’]/ol/li[3]/a”).click()
# Click on sub-category link
driver.find_element_by_xpath(“//*[@id=’top’]/body/div/div[2]/div[2]/div/div[2]/ul/li[4]/a/span”).click()
# Click on product image
driver.find_element_by_xpath(“//*[@id=’product-collection-image-374']”).click()
# Click Checkout button
driver.find_element_by_xpath(“//*[@id=’checkout-button’]/span/span”).click()
driver.find_element_by_id(“billing:firstname”).clear()
driver.find_element_by_id(“billing:firstname”).send_keys(“selenium”, RANDINT, “_fname”)
driver.find_element_by_id(“billing:lastname”).clear()
driver.find_element_by_id(“billing:lastname”).send_keys(“selenium”, RANDINT, “_lname”)
# Click Place Order
driver.find_element_by_xpath(“//*[@id=’order_submit_button’]”).click()
def is_element_present(self, how, what):
try: self.driver.find_element(by=how, value=what)
except NoSuchElementException as e: return False
return True
try: self.driver.find_element(by=how, value=what)
except NoSuchElementException as e: return False
return True
def is_alert_present(self):
try: self.driver.switch_to_alert()
except NoAlertPresentException as e: return False
return True
try: self.driver.switch_to_alert()
except NoAlertPresentException as e: return False
return True
def close_alert_and_get_its_text(self):
try:
alert = self.driver.switch_to_alert()
alert_text = alert.text
if self.accept_next_alert:
alert.accept()
else:
alert.dismiss()
return alert_text
finally: self.accept_next_alert = True
try:
alert = self.driver.switch_to_alert()
alert_text = alert.text
if self.accept_next_alert:
alert.accept()
else:
alert.dismiss()
return alert_text
finally: self.accept_next_alert = True
def tearDown(self):
self.driver.quit()
self.assertEqual([], self.verificationErrors)
self.driver.quit()
self.assertEqual([], self.verificationErrors)
if __name__ == “__main__”:
unittest.main()
To run this script, you first need to install the selenium Python package:
unittest.main()
To run this script, you first need to install the selenium Python package:
$ sudo pip install selenium
Then run the script (called selenium_webdriver_new_user.py in my case):
Then run the script (called selenium_webdriver_new_user.py in my case):
$ python selenium_webdriver_new_user.py
After hopefully not so long of a wait, you should see a successful test run:
After hopefully not so long of a wait, you should see a successful test run:
$ python selenium_webdriver_new_user.py
.
— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —
Ran 1 test in 29.317s
A few notes regarding Selenium WebDriver scripts.
I was stumped for a while when I was trying to use the “find_element_by_id” form of finding an HTML element on a Web page. It was working fine in Selenium IDE, but Selenium WebDriver couldn’t find that element. I had to resort to finding all elements via their XPath id using “find_element_by_xpath”. Fortunately, Chrome for example makes it easy to right click an element on a page, choose Inspect, then righ click the HTML code for the element and choose Copy->Copy XPath to get their id which can then be pasted in the Selenium WebDriver script.
I also had to use time.sleep(N) (where N is in seconds at least for Python) to wait for certain elements of the page to load asynchronously. I know it’s not best practices, but it works.
.
— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —
Ran 1 test in 29.317s
A few notes regarding Selenium WebDriver scripts.
I was stumped for a while when I was trying to use the “find_element_by_id” form of finding an HTML element on a Web page. It was working fine in Selenium IDE, but Selenium WebDriver couldn’t find that element. I had to resort to finding all elements via their XPath id using “find_element_by_xpath”. Fortunately, Chrome for example makes it easy to right click an element on a page, choose Inspect, then righ click the HTML code for the element and choose Copy->Copy XPath to get their id which can then be pasted in the Selenium WebDriver script.
I also had to use time.sleep(N) (where N is in seconds at least for Python) to wait for certain elements of the page to load asynchronously. I know it’s not best practices, but it works.
1 comment:
Lack of below command:
apt-get install -y xorg xvfb firefox dbus-x11 xfonts-100dpi xfonts-75dpi xfonts-cyrillic
anw, thanks so much!
Post a Comment