Using the Python Library
def do_selenium_stuff():
driver = WebDriver(
command_executor='http://localhost:4444/wd/hub',
desired_capabilities=DesiredCapabilities.FIREFOX
)
driver.implicitly_wait(10)
driver.get('http://yelp.com')
for handle in driver.window_handles:
driver.switch_to_window(handle)
driver.refresh()
img = driver.get_screenshot_as_png()
element = driver.find_element_by_css_selector('.foo .bar')
element = driver.find_element_by_css_selector('.foo .bar')
element.click()
element.send_keys('foo bar')
sub_element = element.find_element_by_id('sub-element')
POST /session
desiredCapabilities: {"browser": "firefox", "version": "23.0"}
POST /session/2c34ac26-2d8c-4bfb-9210-3038f7c92252/element
using: "css selector"
value: ".foo input.bar"
GET /session/2c34ac26-2d8c-4bfb-9210-3038f7c92252/element/0/value
value: "Text to enter"
$ sandbox testify yelp.tests.write_a_review
def create_fixtures(email, password):
user = User.create(email, password)
business = Business.create()
return user, business
def perform_login(driver, user):
driver.get('http://dev.yelp.com/login')
driver.find_element_by_id('email').send_keys(user.email)
driver.find_element_by_id('password').send_keys(user.password)
driver.find_element_by_css_selector('button[type=submit]').click()
def perform_write_a_review(driver, rating, review_text):
driver
.find_element_by_css_selector(
'li:nth-child('+ rating +') input[type=radio]')
.click()
driver
.find_element_by_css_selector('input[name=review]')
.send_keys(review_text)
driver.find_element_by_id('submit').click()
def assert_user_wrote_review(user, business, review_text, rating):
review = business.reviews[0]
assert review.text == review_text
assert review.rating == rating
assert review.user == user
def test_write_a_review(driver):
email = 'kstruys@yelp.com'
password = 'password'
review_text = 'This place is awesome!'
rating = 5
user, business = create_fixtures(email, password)
perform_login(driver, user)
assert_user_logged_in(driver, user)
driver.get('http://dev.yelp.com/writeareview/' + business.id)
perform_write_a_review(driver, rating, review_text)
assert 'Your Review has been posted' in
driver.find_element_by_id('alert-message')
assert_user_wrote_review(user, business, review_text, rating)
API To Widgets
form = driver.find_element_by_css_selector('.signup-form')
signup_form = SignupForm(form)
signup_form.update(
first_name="Ken",
last_name="Struys"...
)
signup_form.submit()
API To Pages
class LoginPage {
public LoginPage(WebDriver driver) {
this.driver = driver;
if (!"Login".equals(driver.getTitle())) {
throw new IllegalStateException("Not the login page");
}
}
public Homepage login(String username, String password) {
this.fill_form(username, password);
this.submit();
return new Homepage(this.driver);
}
}
Stage Testing is Tedious, Boring and Error Prone
Write tests as a QA employee, not an engineer
We're hiring
yelp.com/careers