Hi readers, after a long time with busy projects as well as my laziness, I did not practice automation testing regularly. I also forgot a lot of basic knowledge. After a challenging campaign I came back, today I will learn about Automation test again. This article is for first-time contacts as well as those who want to follow the road of automated testing. The main purpose is to cultivate myself the basic knowledge to serve my work and future career. My post may be similar or similar to some articles on the same topic on some other forums. However, I will write with the language closest to, most understandable, hope that you will help in some part of the process of self-learning automation test by yourself.
The first is the definition
What is automated testing?
According to wikipedia
Automated testing is the use of special software (separate from the software being tested) to perform test scenarios and compare actual results with expected results. Automated testing can automate a number of repetitive but necessary tasks in a formalized testing process, or as additional tests that are difficult to perform manually. Automated testing is important for release and ongoing testing.
What is selenium
SELENIUM is a free, open source automated testing framework, used to authenticate web applications on different browsers and platforms. You can use many programming languages like java, C #, Python … to create Selenium test scripts. Testing is done using the Selenium tool commonly known as Selenium Testing
There are many types of automated testing using Selenium, in this article I will only talk about Selenium WebDriver because it’s simple and it’s enough for me.
Prepare the environment
There are a lot of pages about setting up environment now, so I won’t go back. You can refer to this page: https://www.guru99.com/installing-selenium-webdriver.html
Practice
Once the environment is complete, let’s try some basic code
Problem 1 : Check if the title of the web page is correct with the expected results. Try it on youtube.com
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | package Package1; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; public class MyClass { public static void main(String[] args) throws InterruptedException { // khai báo và khởi tạo đối tượng/ biến System.setProperty("webdriver.chrome.driver", "C:\Users\pham.thi.thu.hang\Documents\Chrome driver\chromedriver_win32\chromedriver.exe"); WebDriver driver = new ChromeDriver(); String url = "https://www.youtube.com/"; driver.get(url); String expectedTitle = "YouTube"; // getTitle() hàm lấy title của trang web String actualTitle = driver.getTitle(); // So sánh kết quả thực tế với mong đợi if (expectedTitle.contentEquals(actualTitle)) { System.out.print("Test Passed!"); } else { System.out.print("Test Failed!"); } driver.close(); } } |
Run the above class and the result will be as follows:
In addition to checking the website title, we also have to check many elements inside the website. So how to get each element in our website to the math problem 2.
Problem 2 : Try searching for a song on Youtube
There are 2 issues that need clarification
- How to know which textbox to search?
- How to click the search button to find results
- There are many ways to search for an element in a web page
Variation | Description | For example |
---|---|---|
By.className | Search for an element based on the value of the “class” attribute | findElement (By.className (“someClassName”)) |
By.cssSelector | Search for the element based on the CSS Selector tool below the driver | findElement (By.cssSelector (“input # email”)) |
By.id | Search for elements based on the value of the “id” attribute | findElement ( By.id (“someId”)) |
By.linkText | Search for exact elements based on the displayed linkText | findElement (By.linkText (“REGISTRATION”)) |
By.name | Search for elements based on the value of the attribute “male” | findElement ( By.name (“someName”)) |
By.partialLinkText | Locate the components that contain the partialLinkText | findElement (By.partialLinkText (“REG”)) |
By.tagName | Locate the components based on the tag name | findElement (By.tagName (“div”)) |
By.xpath | Locate components based on the XPath path | / html [1] / body [1] / table [1] |
- Do the maths
- To get the element that is the search box textbox, move the mouse on the test part to get => F12 (or right-click and select Inspect element). From here, let’s get the characteristics of that element. You can install the Chropath tool in the extension for Crhrome to get the element more accurately.
- For example, get the correct element via the Chropath tool
To solve this problem, I will use XPath to get the element
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | public class MyClass { public static void main(String[] args) throws InterruptedException { System.setProperty("webdriver.chrome.driver", "C:\Users\pham.thi.thu.hang\Documents\Chrome driver\chromedriver_win32\chromedriver.exe"); WebDriver driver = new ChromeDriver(); String url = "https://www.youtube.com/"; driver.get(url); // Tìm phần tử ô textbox WebElement textbox = driver.findElement(By.xpath("//input[@id='search']")); // Truyền giá trị cần tìm vào ô textbox textbox.sendKeys("How far I'll go"); //Tìm và click vào button Tìm kiếm WebElement buttonSearch = driver.findElement(By.xpath("//button[@id='search-icon-legacy']")); buttonSearch.click(); } } |
The results obtained when the Run class is as follows:
In the following article, I will write about more practical and difficult problems. Please read the References https://www.guru99.com/selenium-tutorial.html