diff --git a/.gitpod.yml b/.gitpod.yml index aba3dac..386ec69 100644 --- a/.gitpod.yml +++ b/.gitpod.yml @@ -1,6 +1,6 @@ # List the start up tasks. Learn more https://www.gitpod.io/docs/config-start-tasks/ tasks: - - init: pip install selenium && export PYTHONWARNINGS="ignore:Unverified HTTPS request" # runs during prebuild - command: python lambdatest.py + - init: pip install -r requirements.txt && export PYTHONWARNINGS="ignore:Unverified HTTPS request" # runs during prebuild + command: python autoHealingEnableDemo.py && python autoHealingDisableDemo.py diff --git a/README.md b/README.md index 378a9ba..be5ee50 100644 --- a/README.md +++ b/README.md @@ -44,13 +44,13 @@ Before you can start performing **Python** automation testing with **Selenium**, **Step 1:** Clone the LambdaTest’s python-selenium-sample repository and navigate to the code directory as shown below: ```bash -git clone https://github.com/LambdaTest/python-selenium-sample -cd python-selenium-sample +git clone -b Python-AutoHealing-Demo https://github.com/LambdaTest/python-selenium-sample.git +cd Python-AutoHealing-Demo ``` **Step 2:** Download the driver from the link, or you can use **pip** to install it. ```bash -pip install selenium +pip install -r requirements.txt export PYTHONWARNINGS="ignore:Unverified HTTPS request" //Disable ssl warning ``` @@ -74,7 +74,18 @@ Make sure you have your LambdaTest credentials with you to run test automation s ## Run Your First Test ->**Test Scenario**: The [lambdatest.py](https://github.com/LambdaTest/python-selenium-sample/blob/master/lambdatest.py) sample script tests a simple to-do application with basic functionalities like mark items as done, add items in a list, calculate total pending items etc. +>**Test Scenario**: This test automates the process of searching for a book titled "Python Programming" in the Books category on Amazon.in using the LambdaTest platform. + +**📋 Test Steps** +- The script initializes a remote WebDriver session on LambdaTest with the desired capabilities (Windows 10, Chrome browser). +- The Amazon.in website is loaded. +- **The script updates the ID attributes of the search submit button and search textbox.** +- The search category dropdown is clicked to reveal the list of categories. +- The 'Books' category is selected from the dropdown. +- The term "Python Programming" is entered into the search textbox. +- The search button is clicked to initiate the search. + +With `AutoHeal` Enable, the test scenario passed even after updating the ID attribute of submit button. ### Configuration Of Your Test Capabilities @@ -83,28 +94,34 @@ Make sure you have your LambdaTest credentials with you to run test automation s The capabilities object in the above code are defined as: ```python -capabilities = { - "build": "your build name", - "name": "your test name", - "platformName": "Windows 10" - "browserName": "Chrome", - "browserVersion": "latest", + # Desired capabilities for the test + desired_caps = { + 'LT:Options': { + "build": "Auto Heal Demo", + "name": "Auto Heal Enabled", + "platformName": "Windows 10", + "autoHeal": 'true', + }, + "browserName": "Chrome", + "browserVersion": "latest", + } } ``` -You can generate capabilities for your test requirements with the help of our inbuilt [Desired Capability Generator](https://www.lambdatest.com/capabilities-generator/?utm_source=github&utm_medium=repo&utm_campaign=python-selenium-sample). +You can generate capabilities for your test requirements with the help of our inbuilt [Desired Capability Generator](https://www.lambdatest.com/capabilities-generator/?utm_source=github&utm_medium=repo&utm_campaign=python-autoheal-sample). ### Executing The Test -**Step 5:** You would need to execute the below command in your terminal/cmd. +**Execution Steps:** +1. Install the required libraries: + ``` + pip install -r requirements.txt + ``` -```bash -python lambdatest.py -``` -For python3 use -```bash -python3 lambdatest.py -``` +3. Run the test: + ``` + python autoHealingEnableDemo.py && python autoHealingDisableDemo.py + ``` Your test results would be displayed on the test console (or command-line interface if you are using terminal/cmd) and on LambdaTest automation dashboard. diff --git a/autoHealingDisableDemo.py b/autoHealingDisableDemo.py new file mode 100644 index 0000000..92140ac --- /dev/null +++ b/autoHealingDisableDemo.py @@ -0,0 +1,100 @@ +# Import necessary libraries +import time +import unittest +import os +from selenium import webdriver +from selenium.webdriver.common.by import By + +# Fetching environment variables for LambdaTest authentication +username = os.getenv("LT_USERNAME") +access_key = os.getenv("LT_ACCESS_KEY") + +# Define the test class which inherits from unittest.TestCase +class AutoHealDemoTest(unittest.TestCase): + + # This method will be executed before every test method + def setUp(self): + # Desired capabilities for the test + desired_caps = { + 'LT:Options': { + "build": "Auto Heal Demo", + "name": "Auto Heal Disable", + "autoHeal": 'false', + "geoLocation": "IN" + }, + "browserName": "Chrome" + } + + # Initialize the remote webdriver for LambdaTest + self.driver = webdriver.Remote( + command_executor="http://{}:{}@hub.lambdatest.com/wd/hub".format( + username, access_key), + desired_capabilities=desired_caps) + + # This method will be executed after every test method + def tearDown(self): + # Close the browser session + self.driver.quit() + + # Method to update locators on the page + def updateLocators(self, driver): + print("Updating Page Locators") + # Change the ID of the search submit button + driver.execute_script("document.getElementById('nav-search-submit-button').id = 'changed-search-submit-btn';") + time.sleep(1) + # Change the ID of the search textbox + driver.execute_script("document.getElementById('twotabsearchtextbox').id = 'newsearchtextbtn';") + time.sleep(1) + + # Main test method + def test_demo_site(self): + driver = self.loadAmazonWebsite() + # Uncomment below to update page locators. + self.updateLocators(driver) + self.searchBookAndGoToCart(driver) + + # Method to search for a book and navigate to the cart + def searchBookAndGoToCart(self, driver): + print("Executing Test Script") + try: + # Select the dropdown for search categories + drop_down_btn = driver.find_element(By.ID, "searchDropdownBox") + drop_down_btn.click() + time.sleep(1) + # Select 'Books' from the dropdown + books_btn = driver.find_element(By.XPATH, "//*[@id=\"searchDropdownBox\"]/option[11]") + if books_btn.is_displayed(): + books_btn.click() + # Enter 'Python Programming' in the search box + search_box = driver.find_element(By.ID, "twotabsearchtextbox") + if search_box.is_displayed(): + time.sleep(1) + search_box.send_keys("Python Programming") + time.sleep(1) + # Click the search button + search_button = driver.find_element(By.ID, "nav-search-submit-button") + if search_button.is_displayed(): + time.sleep(1) + search_button.click() + # Mark the test as passed on LambdaTest + driver.execute_script("lambda-status=passed") + return + # Mark the test as failed on LambdaTest if any of the above steps fail + driver.execute_script("lambda-status=failed") + except: + # Mark the test as failed on LambdaTest if there's an exception + driver.execute_script("lambda-status=failed") + + # Method to load the Amazon website + def loadAmazonWebsite(self): + driver = self.driver + print('Loading URL') + driver.get("https://www.amazon.com") + driver.set_page_load_timeout(30) + time.sleep(10) + print("Page Loaded Successfully.") + return driver + +# This block ensures the test runs if this script is executed +if __name__ == "__main__": + unittest.main() diff --git a/autoHealingEnableDemo.py b/autoHealingEnableDemo.py new file mode 100644 index 0000000..67f4c5c --- /dev/null +++ b/autoHealingEnableDemo.py @@ -0,0 +1,100 @@ +# Import necessary libraries +import time +import unittest +import os +from selenium import webdriver +from selenium.webdriver.common.by import By + +# Fetching environment variables for LambdaTest authentication +username = os.getenv("LT_USERNAME") +access_key = os.getenv("LT_ACCESS_KEY") + +# Define the test class which inherits from unittest.TestCase +class AutoHealDemoTest(unittest.TestCase): + + # This method will be executed before every test method + def setUp(self): + # Desired capabilities for the test + desired_caps = { + 'LT:Options': { + "build": "Auto Heal Demo", + "name": "Auto Heal Enabled", + "autoHeal": 'true', + "geoLocation": "IN" + }, + "browserName": "Chrome", + } + + # Initialize the remote webdriver for LambdaTest + self.driver = webdriver.Remote( + command_executor="http://{}:{}@hub.lambdatest.com/wd/hub".format( + username, access_key), + desired_capabilities=desired_caps) + + # This method will be executed after every test method + def tearDown(self): + # Close the browser session + self.driver.quit() + + # Method to update locators on the page + def updateLocators(self, driver): + print("Updating Page Locators") + # Change the ID of the search submit button + driver.execute_script("document.getElementById('nav-search-submit-button').id = 'changed-search-submit-btn';") + time.sleep(1) + # Change the ID of the search textbox + driver.execute_script("document.getElementById('twotabsearchtextbox').id = 'newsearchtextbtn';") + time.sleep(1) + + # Main test method + def test_demo_site(self): + driver = self.loadAmazonWebsite() + # Uncomment below to update page locators. + self.updateLocators(driver) + self.searchBookAndGoToCart(driver) + + # Method to search for a book and navigate to the cart + def searchBookAndGoToCart(self, driver): + print("Executing Test Script") + try: + # Select the dropdown for search categories + drop_down_btn = driver.find_element(By.ID, "searchDropdownBox") + drop_down_btn.click() + time.sleep(1) + # Select 'Books' from the dropdown + books_btn = driver.find_element(By.XPATH, "//*[@id=\"searchDropdownBox\"]/option[11]") + if books_btn.is_displayed(): + books_btn.click() + # Enter 'Python Programming' in the search box + search_box = driver.find_element(By.ID, "twotabsearchtextbox") + if search_box.is_displayed(): + time.sleep(1) + search_box.send_keys("Python Programming") + time.sleep(1) + # Click the search button + search_button = driver.find_element(By.ID, "nav-search-submit-button") + if search_button.is_displayed(): + time.sleep(1) + search_button.click() + # Mark the test as passed on LambdaTest + driver.execute_script("lambda-status=passed") + return + # Mark the test as failed on LambdaTest if any of the above steps fail + driver.execute_script("lambda-status=failed") + except: + # Mark the test as failed on LambdaTest if there's an exception + driver.execute_script("lambda-status=failed") + + # Method to load the Amazon website + def loadAmazonWebsite(self): + driver = self.driver + print('Loading URL') + driver.get("https://www.amazon.com") + driver.set_page_load_timeout(30) + time.sleep(10) + print("Page Loaded Successfully.") + return driver + +# This block ensures the test runs if this script is executed +if __name__ == "__main__": + unittest.main() diff --git a/lambdatest.py b/lambdatest.py deleted file mode 100644 index 76a5081..0000000 --- a/lambdatest.py +++ /dev/null @@ -1,93 +0,0 @@ - -import unittest -import os -from selenium import webdriver -from selenium.webdriver.common.by import By - -username = os.getenv("LT_USERNAME") # Replace the username -access_key = os.getenv("LT_ACCESS_KEY") # Replace the access key - - -class FirstSampleTest(unittest.TestCase): - # Generate capabilites from here: https://www.lambdatest.com/capabilities-generator/ - # setUp runs before each test case and - def setUp(self): - desired_caps = { - 'LT:Options': { - "build": "Python Demo", # Change your build name here - "name": "Python Demo Test", # Change your test name here - "platformName": "Windows 11", - "selenium_version": "4.0.0", - "console": 'true', # Enable or disable console logs - "network": 'true', # Enable or disable network logs - #Enable Smart UI Project - #"smartUI.project": "" - }, - "browserName": "firefox", - "browserVersion": "latest", - } - - # Steps to run Smart UI project (https://beta-smartui.lambdatest.com/) - # Step - 1 : Change the hub URL to @beta-smartui-hub.lambdatest.com/wd/hub - # Step - 2 : Add "smartUI.project": "" as a capability above - # Step - 3 : Run "driver.execute_script("smartui.takeScreenshot")" command wherever you need to take a screenshot - # Note: for additional capabilities navigate to https://www.lambdatest.com/support/docs/test-settings-options/ - - self.driver = webdriver.Remote( - command_executor="http://{}:{}@hub.lambdatest.com/wd/hub".format( - username, access_key), - desired_capabilities=desired_caps) - -# tearDown runs after each test case - - - def tearDown(self): - self.driver.quit() - - # """ You can write the test cases here """ - def test_demo_site(self): - - # try: - driver = self.driver - driver.implicitly_wait(10) - driver.set_page_load_timeout(30) - driver.set_window_size(1920, 1080) - - # Url - print('Loading URL') - driver.get("https://stage-lambda-devops-use-only.lambdatestinternal.com/To-do-app/index.html") - - # Let's click on a element - driver.find_element(By.NAME, "li1").click() - location = driver.find_element(By.NAME, "li2") - location.click() - print("Clicked on the second element") - - #Take Smart UI screenshot - #driver.execute_script("smartui.takeScreenshot") - - # Let's add a checkbox - driver.find_element(By.ID, "sampletodotext").send_keys("LambdaTest") - add_button = driver.find_element(By.ID, "addbutton") - add_button.click() - print("Added LambdaTest checkbox") - - # print the heading - search = driver.find_element(By.CSS_SELECTOR, ".container h2") - assert search.is_displayed(), "heading is not displayed" - print(search.text) - search.click() - driver.implicitly_wait(3) - - # Let's download the invoice - heading = driver.find_element(By.CSS_SELECTOR, ".container h2") - if heading.is_displayed(): - heading.click() - driver.execute_script("lambda-status=passed") - print("Tests are run successfully!") - else: - driver.execute_script("lambda-status=failed") - - -if __name__ == "__main__": - unittest.main() \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..27bc3be --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +selenium==3.141.0 \ No newline at end of file