Integration Testing With Selenium - Sloathking/Foolish-Wizardz GitHub Wiki
Integration testing in this context uses a web driver to simulate a user interacting with a page at a high rate of speed. Doing so with a web driver like Selenium yields several challenges since there can be duplicate HTML elements in a page. The IntegrationTestCase class should be started as follows:
What this does is set up the web driver with the designated browser, which is Firefox in this case. Then, it sets the window size to be larger than default, which reduces time code needed to force Selenium to scroll up or down in order to see elements. It also sets the implicit wait time to be 3 seconds, meaning that per query of the HTML, it will wait up to 3 seconds before erroring out, in case elements are still being loaded into the page. Lastly, the web driver navigates to the desired website, which in this case is a web server running locally on port 8000.
The following is an example function which tests the functionality of logging in to add an event.
The find_element() method is very powerful in conjunction with the By operator, which allows searching for information via link text, element names, classes, and much more.
The send_keys() method is meant to be used on text fields, and simply types a given string to fill a field. The opposite of it is the clear() method (not shown here), which deletes everything out of a text field. This is useful for testing the update of a field.
The click() method is used to click on things, such as buttons. Simple as that.
Later in this function, a success_message is located, which is what the function uses to determine whether the test succeeded or not. Assuming you do not want to open several Firefox tabs every time you test, it is recommended to add the quit() method to the end of each function to close the window.
Also of note is the By.XPATH operator, which can be used to parse the HTML as an XML document, which is possible since HTML can be an implementation of HTML. This one is especially useful since it allows you to parse the text between HTML tags. So, if you have a button that cannot be found in any other way, this might come in handy and be more efficient than getting a bunch of elements and iterating through them until you find the right one.
If you have a lot of content on a page, you will need to scroll down to interact with it. This is true when actually using a website as well as when using Selenium. The web driver can only see content that is visible in the window. While a lazy (and admittedly effective) approach is to simply expand the window size, a more elegant solution is to scroll down until a given element (that you're looking to interact with) is visible. One way to do this is with something like:
myElement = browser.find_element(By.<PLACEHOLDER>, "<Placeholder>")
self.browser.execute_script("arguments[0].scrollIntoView({behavior: 'instant', block: 'end'})", myElement)
This will cause the element to be snapped into view.