|
| 1 | +# automation_testing_with_python |
| 2 | + |
| 3 | +This is a framework that will make your life easier in the field of automation testing. The goal of this framework is to making automation testing as easier as python that any one can learn and start using easily. your support will be highly appriciated in this process of making this as the easiest and effective framework in the field of automation testing. Please join us by contributing your inputs and giving this repo a star. |
| 4 | +#### Below are few useful features of this framework |
| 5 | +- Modular Design. |
| 6 | +- Report generation. |
| 7 | +- Mailer to send the report. |
| 8 | +- Events Log. |
| 9 | +#### Prerequisite |
| 10 | +- High learning sprit towards automation |
| 11 | +- Basic knowledge in python |
| 12 | + |
| 13 | +## Quick Start |
| 14 | +- ##### Clone Framework |
| 15 | + <pre> |
| 16 | + git clone https://github.com/jagwithyou/automation-testing-python-selenium.git</pre> |
| 17 | +- ##### Copy the web_ui_testing folder to your project location and open command prompt from there. |
| 18 | + |
| 19 | +- ##### Create and activate virtual environment |
| 20 | + <pre> |
| 21 | + python -m venv ENVIRONMENT_NAME |
| 22 | + ENVIRONMENT_NAME\scripts\activate</pre> |
| 23 | +- ##### Install required Python Packages |
| 24 | + <pre> |
| 25 | + pip install -r requirements.txt</pre> |
| 26 | +- ##### Run the test |
| 27 | + <pre> |
| 28 | + pytest</pre> |
| 29 | +- ##### Work on project |
| 30 | + Congratulations! framework is ready you can add new testcases now. |
| 31 | + |
| 32 | +## Folder Structure |
| 33 | +- web_ui_testing |
| 34 | + - Locators |
| 35 | + - src |
| 36 | + - Drivers |
| 37 | + - logs |
| 38 | + - Reports |
| 39 | + - Utility |
| 40 | + - Tests |
| 41 | + - Config.py |
| 42 | + - Conftest.py |
| 43 | + - api-testing |
| 44 | + - releasing soon |
| 45 | + |
| 46 | +## Web UI Testing: |
| 47 | +### Folder Description |
| 48 | +**Driver** |
| 49 | +- As selenium supports different drivers for different browsers, this folder contains the web drivers that we are going to use on our project. |
| 50 | + Ex:- For chrome browser:- chomedriver.exe |
| 51 | + |
| 52 | +**Locators** |
| 53 | +- Locator is the unique identifier for any element using which we can find the element. |
| 54 | +- Locators can be formed: |
| 55 | + 1) By X-path, |
| 56 | + 2) By CSS |
| 57 | + 3) By ID |
| 58 | + 4) By attribute |
| 59 | + |
| 60 | +- Let's take a example of a submit button whose xpath is //button[@type='submit |
| 61 | +- The locator for the submit button will be: |
| 62 | + SUBMIT_BUTTON = (By.XPATH, "//button[@type='submit']") |
| 63 | + |
| 64 | + |
| 65 | +**Logs** |
| 66 | +- Contains the test execution logs |
| 67 | + |
| 68 | +**Reports** |
| 69 | +- Contains the test case execution report file after test case execution. |
| 70 | + |
| 71 | +**Tests** |
| 72 | +- Contains test cases |
| 73 | +- All the functionality and UI test cases should be created inside this folder |
| 74 | +- For parallel test case execution all the test case prefix should be uniform. |
| 75 | +- To test the user login create a test case as "test_user_login.py". |
| 76 | + |
| 77 | + |
| 78 | +**Utility** |
| 79 | +- this is the utility folder where you should keep your utilities. You can get a large collection of utilities from the [Utilities_repo](../../utilities) |
| 80 | + |
| 81 | +**Config.py** |
| 82 | +- A config.py (configuration) file contains all the settings that you can change as per your requirements. |
| 83 | + |
| 84 | + |
| 85 | +**Conftest.py** |
| 86 | +- conftest is the test configuration file for pytest using which we can change the way pytest is working. From adding the setup and teardown to every test case to import external plugins or modules we can configure these easily. |
| 87 | + |
| 88 | + |
| 89 | +### How to work with this framework |
| 90 | +Suppose you want to test the google search bar (which you have seen in the demo; you can relate the flow with the existing code to understand better). |
| 91 | + |
| 92 | +#### **Step-1** |
| 93 | +As we want to test google, the project url will be google.com. you have to assign this to the BASE_URL variable. |
| 94 | +<pre> |
| 95 | +BASE_URL = "https://www.google.com/" |
| 96 | +</pre> |
| 97 | + |
| 98 | +#### **Step-2** |
| 99 | +Now we have to create a locator file for maintaining all the locators of a page. For a better use create individual locator files for each of the pages of your project. In our case we have created the locator file named google_homepage_locator and declared the locator inside this. |
| 100 | +<pre> |
| 101 | +from selenium.webdriver.common.by import By |
| 102 | + |
| 103 | +SEARCH_BAR = (By.NAME, "q") |
| 104 | +</pre> |
| 105 | + |
| 106 | +#### **Step-3 - Perform the Action** |
| 107 | +Now we are ready to write the test script. For that create a file inside tests folder and name it as test_FILENAME.py. As you can rightly guess each test script file should start with ***test_ |
| 108 | + |
| 109 | +Inside the script the code looks like below. |
| 110 | +<pre> |
| 111 | +from tests import BaseClass |
| 112 | +from locator.google_homepage_locator import * |
| 113 | +from selenium.webdriver.common.keys import Keys |
| 114 | + |
| 115 | +class TestGooglePage(BaseClass): |
| 116 | + def test_1_search(self): |
| 117 | + ''' This is a test case to test the Google Search Page page. ''' |
| 118 | + #Instantiating the logger |
| 119 | + self.log().info("Google Search Page Test Started") |
| 120 | + #accessing the search field and sending text to that |
| 121 | + self.get_element(SEARCH_BAR).send_keys('Automation Testing Python Selenium') |
| 122 | + self.get_element(SEARCH_BAR).send_keys(Keys.RETURN) |
| 123 | + #logging the test success |
| 124 | + self.log().info("Test Success") |
| 125 | + assert True |
| 126 | + |
| 127 | +</pre> |
| 128 | + |
| 129 | +As you can see first we have declared a class that is inherited from the BaseClass and inside it we are writing the test cases. you can assume this class as the ***TestSuite*** and each of the methods as the testcases inside the test-suite. you can write number of testcases inside this class. All the test cases should start as ***test_*** and they are executed alphabetically. so if you want any order of execution then please mention as ***test_1_***. You can give any number (1,2,3) and they will be executed with this order. |
| 130 | + |
| 131 | +The next line is for log if you don't want log you can skip it. After that the operation starts. give your attention to the below three lines. |
| 132 | + |
| 133 | +<pre> |
| 134 | +#accessing the search field and sending text to that |
| 135 | +self.get_element(SEARCH_BAR).send_keys('Automation Testing Python Selenium') |
| 136 | +self.get_element(SEARCH_BAR).send_keys(Keys.RETURN) |
| 137 | +</pre> |
| 138 | + |
| 139 | +1. self.get_element() - it is a method that takes the locator and return the element object to perform operation. |
| 140 | +2. send_keys() - This sends some value to the object. Here it is sending the Text to the search bar of google. These are pytest derived methods and you can get the list from the pytest documentation. |
| 141 | +3. Keys.RETURN - It works like we are clicking the Enter Key after writing our query. |
| 142 | + |
| 143 | +#### Step-4 - Running the script |
| 144 | +We can run the complete testing by using the below command |
| 145 | +<pre> |
| 146 | +pytest |
| 147 | +</pre> |
| 148 | +If you want to run in a different browser then you can add one more argument here like below. |
| 149 | +<pre> |
| 150 | +pytest --browser_name firefox |
| 151 | +</pre> |
| 152 | + |
| 153 | +Awesome we have done this. Similarly you can follow step 2,3,4 to write automation for each of the field. |
0 commit comments