Friday, September 1, 2017

Selenium Python Object Repository - Using Lambda

in the last post I discussed how to create object repository using decorator. if you haven't read the post, please read it here

I am going to take the same example to demonstrate how to use lambda (anonymous function).

from selenium import webdriver
from selenium.webdriver.common.by import By


class TestGoogle(object):
    
    search_input = lambda self: self.find_element(By.NAME, "q")

    def test_search(self):
        self.driver = webdriver.Firefox()
        self.driver.get("https://google.com")
        self.search_input().send_keys("selenium python")
Pros of Using
  • all the elements are in one place in the file (class), so if any element change it's easy and fast
  • it's a function, so every time a object would be created, you should never get StaleElementRefereceException
Cons of Using
  • You will have to keep in mind it's a function not a variable
  • Every time a new object would be created, so it could be a overhead.