{"id":7641,"date":"2023-11-06T20:00:43","date_gmt":"2023-11-06T14:30:43","guid":{"rendered":"https:\/\/python-programs.com\/?p=7641"},"modified":"2023-11-10T12:20:00","modified_gmt":"2023-11-10T06:50:00","slug":"synchronization-in-selenium-python","status":"publish","type":"post","link":"https:\/\/python-programs.com\/synchronization-in-selenium-python\/","title":{"rendered":"Synchronization in Selenium Python | Synchronizing Test"},"content":{"rendered":"
In our earlier chapters, we have learned how do we automate scenarios, identify objects uniquely on a web page and perform actions on them. During the automation of scenarios, it is important that we synchronize our tests. So the time it takes for the web application to process, the automation command should match with the speed at which the command is sent by the script to the application.<\/p>\n
Structure<\/strong><\/p>\n Objective<\/strong><\/p>\n In this chapter, we will leam how we can establish synchronization in our tests so that we ensure our tests are not flaky. Reliable execution is crucial during testing cycles as they help save time, and ensure reliability in test automation exercises. We will learn how we implement that concept in our test scripts.<\/p>\n Synchronization<\/strong><\/p>\n If we have not implemented synchronization in our tests, then our tests may hover between passing and failing, resulting in flaky tests. To avoid this we should synchronize our tests so that we have basic reliability in our test behavior. To achieve it we have to apply synchronization in our tests. Implicit wait<\/strong><\/p>\n The implicit wait is a global wait, which applies to every statement in the written test script. An implicit wait, when implemented in the script, tries to find the element on the web page. It will keep polling the web page until the element is found, or till the time is over. If the element is not found within the provided implicit wait, we get an exception NoSuchElementException.<\/p>\n In the following program, we will implement implicit wait:<\/p>\n The commanding self .driven.implicity_wait(30) will apply the wait on every find element command used in the program. So for every statement, it will wait for 30 seconds for the object to appear with the given By locator. It will keep polling the website until it finds the object. If the object is found, the action will be performed on the object. Else after the 30 seconds are over, we will get an exception.<\/p>\n Explicit wait<\/strong><\/p>\n The explicit wait is basically a local wait which can be implemented either as:<\/p>\n \u2022 Static wait:<\/strong> It is a forced wait, which is introduced by using time.sleep(n seconds) in the code line, whenever we wish to wait in the code for n number of seconds. It is not advisable to use static wait in the code because we generally do not know if the time allocated to wait is less or more. We cannot provide a lot of time to wait in the code, because that will delay our test automation script, and if we provide very little time, it may result in a flaky test, so such waits are generally unreliable and not advisable.<\/p>\n \u2022 Dynamic wait:<\/strong> The dynamic wait is implemented in the code with the help of a class called as WebDriverWait. This class has a method called as until ( ). In this method, we pass an event which may occur and the time units for which we wish to wait for that event to occur. So in this method of WebDriverWait, we either wait for an event to occur or it times out. The exception we get in here, in case the event doesn’t occur, is TimedOutException. But in case the event has occurred, we do not wait for the entire amount of time to finish, we get out of the until loop as soon as it’s finished.<\/p>\n So, we will have a look at two sets of code here. In the first example, we will run a positive scenario of login logout using WebDriverWait. In the second example, we will execute a negative scenario in which we will fail forcefully by passing wrong object information in the code. So the method will wait for the timeout object to appear:<\/p>\n The code lines using which we have implemented explicit wait are: WebDriverWait(driver, 10).until( In the preceding until( ) method, we take an input argument called EC. presence_of_element_located(By.LINK_TEXT, ” Log Off”), here the EC is basically the class called as expected_ conditions exported from selenium.web driver.support import expected_conditions asEC. It has a lot of methods available with it which can help trace an event. In the preceding code, we have used a method presence_of_element_located, so this will basically look for the link with the text Log Off, for 10 seconds. If it finds the link within 10 seconds it will exit the until loop and execute the next command, which is clicking on the link, otherwise, it will timeout and throw a TimedOutException.<\/p>\n Let us try another code example, where we will give a bad locator for the Log Off link, causing the WebDriver wait method to timeout:<\/p>\n WebOriverHait(driver, 10}.until( The exception which is thrown is an follows: Conclusion<\/strong><\/p>\n In this chapter, we have seen how we can use the concept of waits, implicit and explicit wait, in our test automation code and ensure that the scripts are reliable during execution time. By implementing the concept of synchronization we have tried to achieve less flaky tests and introduce predictability in them during execution time. In the next chapter, we will discuss advanced types of web elements like table, dropdown, alerts, and frame. We will see how to automate these elements and what all methods are associated with them.<\/p>\n Related Articles:<\/strong><\/p>\n Selenium Python – Synchronizing Test In our earlier chapters, we have learned how do we automate scenarios, identify objects uniquely on a web page and perform actions on them. During the automation of scenarios, it is important that we synchronize our tests. So the time it takes for the web application to process, the automation …<\/p>\n\n
\nThere are two kinds of synchronization techniques that we use in test scenarios of Selenium:<\/p>\n\n
from selenium import webdriver\r\nimport unittest\r\n\r\nclass Login(unittest.Testcase):\r\n def setup(self):\r\n self.driver = webdriver.chrome(executable_path=\"D:\\Eclipse\\BPB\\seleniumpython\\seleniumpyhon\\drivers\\chromedriver.exe\")\r\n self.base_url = \"http:\/\/practice.bpbonline.com\/catalog\/index.php\"\r\n\r\ndef test_login(self):\r\n driver=self.driver\r\n driver.get(self.base_url)\r\n driver.find_element_by_link_test(\"My Account\").click( )\r\n driver.find_element_by_name(\"email_address\").clear( )\r\n driver.find_element_by_name(\"email_address\").send_keys(\"bpb@bpb.com)\r\n driver.find_element_by_name(\"password\").clear( )\r\n driver.find_element_by_name(\"password\").send_keys(\"bpb@123\")\r\n driver.find_element_by_id(\"tab1\").click( )\r\n driver.find_element_by_lik_text(\"log off\").click( )\r\n driver.find_element_by_lik_text(\"continue\").click( )\r\n\r\ndef tearDown(self):\r\n self.driver.quit( )\r\n\r\nIf_name_==_main_\":\r\n unittest.main( )<\/pre>\n
from selenium import webdriver\r\nfrom selenium.webdriver.common.by import By\r\nfrom selenium.webdriver.support.ui import webDriverwait\r\nfrom selenium.webdriver.support import expected_conditions as EC\r\nimport unitest\r\n\r\nclass Login(unitest.TestCase):\r\n def setup(self):\r\n self.driver = webdriver.chrome(executable_path=\"D:\\Eclipse\\BPB\\seleniumpython\\seleniumpython\\drivers\\chromedriver.exe\")\r\n self.base_url=\"http:\/\/practice.bpbonline.com\/catalog\/index.php\"\r\n\r\ndex test_login(self):\r\n driver=self.driver\r\n driver.get(self.base_url)\r\n driver.find_element_by_link_text(\"My Account\").click( )\r\n driver.find_element_by_name(\"email_address\").clear( )\r\n driver.find_element_by_name(\"email_address\").send_keys(\"bpb@bpb.com\")\r\n driver.find_element_by_name(\"password\").clear( )\r\n driver.find_element_by_name(\"password\").send_keys(\"bpb@123\")\r\n driver.find_element_by_id(\"tab1\").click( )\r\n webdriverwait(driver, 1e).until(\r\n Ec.presence_of_element_located((By.LINK_TEXT, \"log off\")))\r\n driver.find_element_by_link_text(\"log off\").click( )\r\n driver.find_element_by_link_text(\"continue\").click( )\r\n\r\ndef tearDown(self):\r\n self.driver.quit( )\r\n\r\nif _name_ ==\" _main_ \":\r\n unitest.main( )<\/pre>\n
\nEC.presence_of_element_located((By.LINK_TEXT, “Log Off”)))<\/p>\n
\nEC:presence_of_elenent_located((By.LIHK_TEXT, “Log \u00a3\u00a3\u201d})) driver.find_eleinent_by_link_text(“Log Off”).click()<\/p>\n
\nraise TiraeoutException(message, screen., stacktrace)
\nselenium.common,exceptions.TimeoutrExceptions Message;<\/p>\n\n