Best Python code snippet using Airtest
test_integration.py
Source:test_integration.py
...52 remove_leftover_files(self.CREATED_FILES)53 self.CREATED_FILES.clear()54 def test_backend_server_available(self):55 self.open(self.BACKEND_ADDRESS)56 self.assert_text('{"Hello":"World"}')57 def test_frontend_server_available(self):58 self.open(self.FRONTEND_ADDRESS)59 self.assert_text('Home')60 self.assert_text('About')61 self.assert_text('NormalChat')62 self.assert_text('Todo')63 def test_add_todo_submit1(self):64 """ Add a new to-do entry """65 self.open(self.FRONTEND_ADDRESS)66 self.assert_text('Hello world!')67 self.click('#todo')68 self.assert_text_not_visible('Unable to connect to server - running local mode', timeout=1)69 test_text = 'my amazing test todo text1'70 self.assert_text_not_visible(test_text)71 self.write('#newTodoInput', test_text)72 self.click('#submit1')73 self.assert_text(test_text)74 self.assert_text_not_visible('Unable to connect to server - running local mode', timeout=1)75 def test_add_todo_submit2(self):76 """ Add a new to-do entry, same as above """77 self.open(self.FRONTEND_ADDRESS)78 self.assert_text('Hello world!')79 self.click('#todo')80 self.assert_text_not_visible('Unable to connect to server - running local mode', timeout=1)81 test_text = 'my amazing test todo text2'82 self.assert_text_not_visible(test_text)83 self.write('#newTodoInput', test_text)84 self.click('#submit2')85 self.assert_text(test_text)86 self.assert_text_not_visible('Unable to connect to server - running local mode', timeout=1)87 def test_add_todo_submit3(self):88 """ Add a new to-do entry, same as above """89 self.open(self.FRONTEND_ADDRESS)90 self.assert_text('Hello world!')91 self.click('#todo')92 self.assert_text_not_visible('Unable to connect to server - running local mode', timeout=1)93 test_text = 'my amazing test todo text3'94 self.assert_text_not_visible(test_text)95 self.write('#newTodoInput', test_text)96 self.click('#submit3')97 self.assert_text(test_text)98 self.assert_text_not_visible('Unable to connect to server - running local mode', timeout=1)99 def test_chat_single(self):100 """ Chat with yourself """101 self.open(self.FRONTEND_ADDRESS)102 self.assert_text('Hello world!')103 self.click('#chat')104 my_username = 'beep_boop'105 self.assert_text_not_visible(my_username)106 self.write('#username', my_username)107 self.click('#connect')108 # Send a message by pressing send button109 some_text = 'bla blubb'110 self.write('#chatinput', some_text)111 self.assert_text_not_visible('You')112 self.click('#sendmessage')113 self.assert_text('You')114 self.assert_text(some_text)115 # Send a message by pressing enter116 some_other_text = 'some other text'117 self.write('#chatinput', f'{some_other_text}\n')118 self.assert_text(some_other_text)119 def test_chat_two_people(self):120 """ Make sure chat between 2 people work """121 # Connect with robot1122 self.open(self.FRONTEND_ADDRESS)123 self.click('#chat')124 my_username1 = 'robot1'125 self.write('#username', my_username1)126 self.click('#connect')127 # Send message from robot1128 some_text1 = 'sometext1'129 self.write('#chatinput', some_text1)130 self.click('#sendmessage')131 self.assert_text('You')132 self.assert_text(some_text1)133 # Connect with robot2134 self.open_new_window(True)135 self.open(self.FRONTEND_ADDRESS)136 self.click('#chat')137 my_username2 = 'robot2'138 self.write('#username', my_username2)139 self.click('#connect')140 # Make sure robot1's messages are visible from robot2141 self.assert_text(my_username1)142 self.assert_text(some_text1)143 # Send message from robot2144 some_text2 = 'sometext2'145 self.write('#chatinput', some_text2)146 self.click('#sendmessage')147 self.assert_text('You')148 self.assert_text(some_text2)149 # Make sure robot2's messages are visible from robot1150 self.switch_to_window(0)151 self.assert_text(my_username2)152 self.assert_text(some_text2)153if __name__ == '__main__':154 # This doesnt work anymore with classes, why?155 test = MyTestClass()156 test.setup_method('')157 test.test_backend_server_available()...
pet_fr.py
Source:pet_fr.py
...6class MyTestClass(BaseCase):7 def test_1page(self):8 self.open("https://petfriends1.herokuapp.com/")9 self.highlight("/html/body/div/div/h1")10 self.assert_text("PetFriends", "h1")11 self.highlight("/html/body/nav/a")12 self.assert_link_text("PetFriends")13 self.highlight("/html/body/div/div/div[1]")14 self.assert_text("СоÑиалÑÐ½Ð°Ñ ÑеÑÑ Ð´Ð»Ñ Ð»ÑбиÑелей живоÑнÑÑ
")15 self.click('/html/body/div/div/div[2]/button') #заÑегиÑÑÑиÑоваÑÑÑÑ1617 def test_2page(self):18 self.open('http://petfriends1.herokuapp.com/new_user')19 self.highlight("/html/body/div/div/form/div[1]/label")20 self.assert_text("УникалÑное имÑ")21 self.type('input[name="name"]', "User")22 self.highlight('//*[@id="NameHelp"]')23 self.assert_text("ÐведиÑе ÑникалÑное Ð¸Ð¼Ñ Ð¿Ð¾Ð»ÑзоваÑелÑ")24 self.clear('input[name="name"]')2526 self.highlight("/html/body/div/div/form/div[2]/label")27 self.assert_text("ÐлекÑÑÐ¾Ð½Ð½Ð°Ñ Ð¿Ð¾ÑÑа")28 self.type('input[name="email"]', "email")29 self.highlight('//*[@id="EmailHelp"]')30 self.assert_text("ÐведиÑе адÑÐµÑ ÑлекÑÑонной поÑÑÑ")31 self.clear('input[name="email"]')3233 self.highlight("/html/body/div/div/form/div[3]/label")34 self.assert_text("ÐаÑолÑ")35 self.type('input[name="pass"]', "pass")36 self.highlight('//*[@id="PassHelp"]')37 self.assert_text("ÐведиÑе паÑолÑ")38 self.clear('input[name="pass"]')3940 self.click_link("У Ð¼ÐµÐ½Ñ Ñже еÑÑÑ Ð°ÐºÐºÐ°ÑнÑ")41 self.type('input[name="email"]', email)42 self.type('input[name="pass"]', password)43 self.click('button[class="btn btn-success"]')44 time.sleep(3)4546 def test_3page(self):47 self.open('http://petfriends1.herokuapp.com/new_user')48 self.click_link("У Ð¼ÐµÐ½Ñ Ñже еÑÑÑ Ð°ÐºÐºÐ°ÑнÑ")49 self.type('input[name="email"]', email)50 self.type('input[name="pass"]', password)51 self.click('button[class="btn btn-success"]')5253 self.highlight('//*[@id="navbarNav"]/ul/li[1]/a')54 self.assert_link_text("Ðои пиÑомÑÑ")55 self.highlight('//*[@id="navbarNav"]/ul/li[2]/a')56 self.assert_link_text("ÐÑе пиÑомÑÑ")57 self.highlight('//html/body/div/div/div[1]')58 self.assert_text("ÐÑе пиÑомÑÑ Ð½Ð°ÑиÑ
полÑзоваÑелей")59 self.highlight('//html/body/div/div/h1')60 self.assert_text("PetFriends", "h1")6162 self.highlight('//html/body/nav/div[2]/button')63 self.assert_text("ÐÑйÑи")64656667 def test_4page(self):68 self.open('http://petfriends1.herokuapp.com/new_user')69 self.click_link("У Ð¼ÐµÐ½Ñ Ñже еÑÑÑ Ð°ÐºÐºÐ°ÑнÑ")70 self.type('input[name="email"]', email)71 self.type('input[name="pass"]', password)72 self.click('button[class="btn btn-success"]')73 self.open('http://petfriends1.herokuapp.com/my_pets')7475 self.highlight("h2")76 self.highlight('/html/body/div[1]/div/div[1]')77 self.assert_text("ÐиÑомÑев:")78 self.assert_text("ÐÑÑзей:")79 self.assert_text("СообÑений:")8081 self.highlight('//*[@id="all_my_pets"]/table/thead/tr/th[1]')82 self.assert_text("ФоÑо")83 self.highlight('//*[@id="all_my_pets"]/table/thead/tr/th[2]')84 self.assert_text("ÐмÑ")85 self.highlight('//*[@id="all_my_pets"]/table/thead/tr/th[3]')86 self.assert_text("ÐоÑода")87 self.highlight('//*[@id="all_my_pets"]/table/thead/tr/th[4]')88 self.assert_text("ÐозÑаÑÑ")89 self.highlight('/html/body/div[1]/div/div[2]/div/button')90 self.assert_text("ÐобавиÑÑ Ð¿Ð¸ÑомÑа")91 self.click('/html/body/div[1]/div/div[2]/div/button')9293 self.highlight('//*[@id="addPetsModalLabel"]')94 self.assert_text("ÐовÑй пиÑомеÑ")95 self.highlight('//*[@id="addPetsModal"]/div/div/div[1]/button/span')96 self.highlight('// *[ @ id = "pet_photo"]')979899 #self.assert_element('img[name="photo"]')100101 self.highlight('//*[@id="addPetsModal"]/div/div/div[2]/form/div[2]/label')102 self.assert_text("ÐÐ¼Ñ Ð¿Ð¸ÑомÑа")103 self.type('//*[@id="name"]', 'pet_name')104105 self.highlight('//*[@id="addPetsModal"]/div/div/div[2]/form/div[3]/label')106 self.assert_text("ÐоÑода")107 self.type('//*[@id="animal_type"]', 'wild animal')108109 self.highlight('//*[@id="addPetsModal"]/div/div/div[2]/form/div[4]/label')110 self.assert_text("ÐозÑаÑÑ, леÑ")111 self.type('//*[@id="age"]', '11')112113114 self.clear('input[name="name"]')115 self.clear('input[name="animal_type"]')116 self.clear('input[name="age"]')117118 self.highlight('//*[@id="addPetsModal"]/div/div/div[3]/button[1]')119 self.assert_text("ÐÑмена")120121 self.highlight('//*[@id="addPetsModal"]/div/div/div[3]/button[2]')122 self.assert_text("ÐобавиÑÑ")123124 self.click('//*[@id="addPetsModal"]/div/div/div[3]/button[1]')125126 time.sleep(3)127128129130
...
test_e2e.py
Source:test_e2e.py
...26class MyTestClass(BaseCase):27 def test_basic_site_display(self):28 """ Check if HOME site is visible """29 self.open(FRONTEND_ADDRESS)30 self.assert_text('Hello world!')31 def test_shows_todos(self):32 """ Check if the to-do site is visible """33 self.open(FRONTEND_ADDRESS)34 self.click('#todo')35 self.assert_text('Unable to connect to server - running local mode')36 def test_add_todo_submit1(self):37 """ Add a new to-do entry """38 self.open(FRONTEND_ADDRESS)39 self.click('#todo')40 self.assert_text('Unable to connect to server - running local mode')41 test_text = 'my amazing test todo text1'42 self.write('#newTodoInput', test_text)43 self.click('#submit1')44 self.assert_text(test_text)45 self.assert_text('Unable to connect to server - running local mode')46 def test_add_todo_submit2(self):47 """ Add a new to-do entry """48 self.open(FRONTEND_ADDRESS)49 self.click('#todo')50 self.assert_text('Unable to connect to server - running local mode')51 test_text = 'my amazing test todo text2'52 self.write('#newTodoInput', test_text)53 self.click('#submit2')54 self.assert_text(test_text)55 self.assert_text('Unable to connect to server - running local mode')56 def test_add_todo_submit3(self):57 """ Add a new to-do entry """58 self.open(FRONTEND_ADDRESS)59 self.click('#todo')60 self.assert_text('Unable to connect to server - running local mode')61 test_text = 'my amazing test todo text3'62 self.write('#newTodoInput', test_text)63 self.click('#submit3')64 self.assert_text(test_text)65 self.assert_text('Unable to connect to server - running local mode')66 def test_example(self):67 url = 'https://store.xkcd.com/collections/posters'68 # Go to url69 self.open(url)70 # Type in input field "xkcd book"71 self.type('input[name="q"]', 'xkcd book')72 # Click the search icon to start searching73 self.click('input[value="Search"]')74 # Assert that there is a header with class "h3" which has text: "xkcd: volume 0"75 self.assert_text('xkcd: volume 0', 'h3')76 # Go to new url77 self.open('https://xkcd.com/353/')78 self.assert_title('xkcd: Python')79 self.assert_element('img[alt="Python"]')80 # Click on <a> element with rel="license"81 self.click('a[rel="license"]')82 # Assert that there is this text on the website visible83 self.assert_text('free to copy and reuse')84 # Click go_back85 self.go_back()86 # Click the "About" link87 self.click_link('About')88 # Assert that there is a header with class "h2" which has text: "xkcd.com"89 self.assert_exact_text('xkcd.com', 'h2')90class MyBenchClass(BaseCase):91 def __init__(self, *args, **kwargs):92 super().__init__(*args, **kwargs)93 self.benchmark: Optional[BenchmarkFixture] = None94 @pytest.fixture(autouse=True)95 def setup_benchmark(self, benchmark):96 """97 Assign the benchmark to a class variable98 For more info see https://pytest-benchmark.readthedocs.io/en/latest/usage.html99 https://github.com/ionelmc/pytest-benchmark/blob/master/tests/test_with_testcase.py100 """101 self.benchmark = benchmark102 def basic_site_display(self):103 """ Check if HOME site is visible """104 self.open(FRONTEND_ADDRESS)105 self.assert_text('Hello world!')106 def test_bench_basic_site_display(self):107 """ Benchmark how fast the site loads """108 self.benchmark(self.basic_site_display)109 def add_todo(self):110 """ Add a new to-do entry """111 self.open(FRONTEND_ADDRESS)112 self.click('#todo')113 self.assert_text('Unable to connect to server - running local mode')114 test_text = 'my amazing test todo text'115 self.write('#newTodoInput', test_text)116 self.click('#submit1')117 self.assert_text(test_text)118 def test_bench_add_todo(self):119 """ Benchmark how fast a to-do can be added """120 self.benchmark(self.add_todo)121if __name__ == '__main__':122 setup_module()...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!