Have you ever wanted to create end to end tests for your app, but just can't find the time to get those tests working. Well, I have tried several frameworks from Cypress, to Selenium. I have always felt like this technology ought to be made simple. Just recently after taking another look at Selenium,
Selenium is an open-source software suite used for automating web browser interactions. It provides a way to simulate user actions and interactions with web applications, allowing developers and testers to automate various tasks, perform functional testing, and ensure the quality and correctness of web applications.
Key features and concepts of Selenium include:
Web Testing Automation: Selenium is primarily used for automating web testing scenarios. It can interact with web elements, fill out forms, click buttons, navigate pages, and validate expected behaviors.
Cross-Browser Compatibility: Selenium supports multiple web browsers, including Google Chrome, Mozilla Firefox, Microsoft Edge, Safari, and more. This allows you to test your web application's compatibility across different browsers.
Programming Language Support: Selenium supports several programming languages, including Java, Python, C#, Ruby, and JavaScript. This enables you to write tests in a language of your choice.
Web Element Locators: Selenium provides various methods for locating web elements on a page, such as by ID, name, XPath, CSS selectors, and more. These locators help identify elements for interaction.
Test Scripts: Selenium test scripts are written to simulate user actions and validate expected outcomes. Test scripts can be executed sequentially or in parallel, depending on your testing needs.
Selenium WebDriver: WebDriver is a component of Selenium that provides a programming interface for interacting with web browsers. It allows you to control browsers programmatically and perform actions like clicking, typing, and navigating.
Test Framework Integration: Selenium can be integrated with various testing frameworks, such as JUnit, TestNG, NUnit, and more, enabling you to organize and manage your tests effectively.
Headless Browsing: Selenium can perform browser automation without a visible user interface. This is useful for running tests in headless mode to improve efficiency and speed.
I found the IDE to be very easy to use in order to quickly create scripts that could be improved by hand later. Here is a very simple example.
First I installed the Chrome Selenium extension.
https://docs.seleniumhq.org/selenium-ide/
I created a simple test using the record button and exported it as a Python test.
Then I installed the Python Webdriver using
apt-get -y install python3.6 apt-get -y install python3-pip pip3 selenium
Next I installed the chromedriver and I used NPM
npm install chromedriver
Now make sure you have Chrome installed. I used Headless on my server.
I may have added source first
apt-get -y install google-chrome-stable
Then I ran the following script with this command.
chmod 777 test.py ./test.py
#! /usr/bin/python3.6 import os from selenium import webdriver from selenium.webdriver.common.keys import Keys from selenium.webdriver.chrome.options import Options from selenium.webdriver.common.by import By chrome_options = Options() chrome_options.add_argument("--headless") chrome_options.add_argument('--no-sandbox') chrome_options.add_argument('--disable-dev-shm-usage') chrome_options.binary_location = '/usr/bin/google-chrome-stable' driver = webdriver.Chrome(executable_path=os.path.abspath("node_modules/chromedriver/bin/chromedriver"), chrome_options=chrome_options) driver.get("http://www.jonclawson.com/") driver.set_window_size(1280, 740) driver.find_element(By.LINK_TEXT, "who").click() assert driver.find_element(By.CSS_SELECTOR, ".title").text == "my name is jon" text = driver.find_element(By.CSS_SELECTOR, ".title").text == "my name is jon" print(text) print('success') driver.close()