# This sample code uses the Appium python client
# pip install Appium-Python-Client
# Then you can paste this into a file and simply run with Python
import time
import random
from appium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait

caps = {}
caps["deviceName"] = "emulator-5554"
caps["platformName"] = "android"
caps["browserName"] = "Chrome"
caps["chromedriverExecutable"] = "/Users/me/webdrivers/chromedriver83"
# caps["appPackage"] = "com.android.chrome"
# caps["appActivity"] = "com.google.android.apps.chrome.Main"
caps["noReset"] = True

driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", caps)

driver.get('https://__SOME WEBSITE__')
rand_num = random.randrange(10000,80000)
time.sleep(5)
elem = driver.find_element_by_css_selector(".register")
elem_first_name = driver.find_element_by_css_selector("#frstname")
elem_last_name = driver.find_element_by_css_selector("#lastname")
elem_email = driver.find_element_by_css_selector("#email")
elem_reg_checkbox = driver.find_element_by_css_selector("#checkTermsConditions")
elem_reg_radio = driver.find_element_by_css_selector("#radioPersonalOrBusiness")
elem_reg_submit = driver.find_element_by_css_selector("#submit")

elem.click()
time.sleep(2)
elem_first_name.send_keys("Brian")
elem_last_name.send_keys("Warner")
elem_email.send_keys("continuousqa+"+str(rand_num)+"@gmail.com")
elem_reg_checkbox.click()
elem_reg_radio.click()
elem_reg_submit.click()

driver.quit()

Although I’ve committed an actual site in the code example above, the code example actually works on a registration form I was running locally.

Appium is a pain in the ass to install and set up… but it’s the only player in town for a free open source solution to mobile testing. To go through the hell of installation, check out my article on that:

Oddly it’s easier to automate native apps, than browsers. Browsers hit all sorts of permission issues, version issues and ultimately it’s a real pain. At the end of the aforementioned article I found a solution to most of this, and various bugs I hit along the way are detailed.

Writing Browser Tests

Using Selenium Webdriver calls, we automate just like we would on a desktop browser.

I did hit a problem with automating with Python… when I attempt to find an element by class/id/name it fails saying it’s an invalid method call. However find by css selector works. Googling around this appears to be an old Selenium bug with python that has been reintroduced. 🙁

If you’re working in Java, JavaScript or some other language, you probably won’t have this issue.

New to Browser Automation?

Using Chrome or Firefox, go through your pages, right click on a button, field or element you need to automate an interaction with. Click the “inspect” sub menu and locate the id/css or name values for each element.

Interacting with elements is usually done by “find_element_by…” methods. Then applying a .click() method.

Sending text is handled through a send_keys method.

Check out Selenium’s docs for more info.

#

No responses yet

Leave a Reply

Your email address will not be published. Required fields are marked *

Recent Comments
Archives
Categories