Selenium automated test – Textbox, checkbox, radio button, submit button, click (Part 2)

Tram Ho

To continue Part 1 of the Selenium Automated Test series. Today we will learn some more complex functions. To prepare for this article you should read part 1 .

Now I will demo on a demo page: Or you can also get any page with login / register functionality to practice okay!

1. TextBox, Submit Button, sendkeys (), click ()

Problem : Please automate the login function on your website Analysis: The login function will usually have a textbox with username, a password box, and a Submit button. First, you have to find the element. of the above items Execute F12 (or inspect element) => get specific points of that element Found an element for each item. Now code and run the program to see the result

The result is as in the image below,

As above, you see I used the sendKeys () function to pass data to the element to be entered and used the submit () function to submit the Login form. So in this article we will note some commonly used functions as follows:

Input BoxsendKeys ()Used to transfer data into text boxes
Input Boxclear ()Delete the current value of the text boxes
Linksclick ()Click the link to load the page
Submit Buttonsubmit ()Submit form, submit button

2. Select CheckBox, Radio Button and Image

For items CheckBox, Radio Button and Image are similar buttons. I will use the click () function to select the value

  • Select CheckBox, Radio Button

When the code will use the click () function to select a value

  • Image Use the click () function to navigate to the link contained in the image

3. Select value from Dropdown

To select the value in the first Dropdown cell, it is still necessary to get the element of the selected cell To select the value in the first Dropdown box, we have to import the import; library import; Then initialize a variable Select to select to that Dropdown Select language = new Select(driver.findElement("user[language]"))); Finally, select the value to select language.selectByVisibleText("English");

In addition to the selectByVisibleText function, we can use some other functions to get values ​​via index, id, value … as follows:

Drop-Down BoxselectByVisibleText () / deselectByVisibleText ()select / deselect an option through the displayed text
selectByValue () / deselectByValue ()select / deselect an option according to the value of the “value” attribute
selectByIndex () / deselectByIndex ()Select / cancel an option in the Index order
isMultiple ()returns TRUE if the Dropdown element allows multiple options at the same time; FAILSE if you can’t select multiple options at once
deselectAll ()Clear all previously selected options

The above is how to pass and select values ​​in commonly used items. In practice, you should do more to master and improve your skills. Hope this article will be helpful for readers.


Share the news now

Source : Viblo