Skip to main content

Working with Dropdownbox elements in Selenium WebDriver

How to select element from a Dropbox

We are going to use Selenium webDriver and chrome driver to test this.

In addition, this test was created on Mac.  I assume you have installed and setup java in your system path.  Also, install Eclipse for jee.

1. First let's take a look at a quick test case:

Test case:

TC_1. Register on http://automationpractice.com/index.php
TC_1.1: Launch hope page -http://automationpractice.com/index.php
TC_1.2 : Click on link ‘Sign in’
TC_1.3 : Under “Create Account” subheading, enter Email address in ‘Email address’ textfield
TC_1.4 : Click on ‘Create an account’ button.

——————————————————————————————

Test data:
Email address: gorgeous12@hotmail.com

http://automationpractice.com/index.php

----------------------------------------------------------------

———————————------------




2. Next, create a maven project in Eclipse

3. Click on your pom.xml, go to the xml section.  Then on your browser, go to maven repository and search for 'selenium':

https://mvnrepository.com/search?q=selenium

copy the maven section as below into your pom.xml -

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  <modelVersion>4.0.0</modelVersion>
  <groupId>com.automationpractice</groupId>  <artifactId>AutomationPractice</artifactId>  <version>0.0.1-SNAPSHOT</version>  <packaging>jar</packaging>
  <name>AutomationPractice</name>  <url>http://maven.apache.org</url>
  <properties>    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  </properties>
  <dependencies>  
     <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>    <groupId>org.seleniumhq.selenium</groupId>    <artifactId>selenium-java</artifactId>    <version>3.6.0</version> </dependency> <!-- https://mvnrepository.com/artifact/org.testng/testng -->
   <dependency>    <groupId>org.testng</groupId>    <artifactId>testng</artifactId>    <version>6.10</version>    <scope>test</scope></dependency>  </dependencies></project>

Save

4. Create a class in the folder:

MavenProject/src/test/java/package

and copy the following codes inside

package com.automationpractice.AutomationPractice;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
/**
 *
 */
public class LaunchCloseApp {
WebDriver driver;
@BeforeClass
public void launchApp() {
//instantiate Chrome Browser driver
System.setProperty("webdriver.chrome.driver", "/Users/tester/Documents/webDrivers/chrome/chromedriver"); driver = new ChromeDriver();
driver.get("http://automationpractice.com/index.php"); //boolean applaunch=driver.getCurrentUrl();
System.out.println("browser has launched"); }
@AfterClass
public void CloseApp() {
driver.quit();
System.out.println("browser has quite");
}


}



You would need to download the chrome driver and make changes to the this path:

//instantiate Chrome Browser driver
System.setProperty("webdriver.chrome.driver", "/Users/tester/Documents/webDrivers/chrome/chromedriver");


Then create another class and copy the following into it :

package com.automationpractice.AutomationPractice;
import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.Select;
import org.testng.annotations.Test;
//import com.automationpractice.*;
public class Register {
LaunchCloseApp lanuchApp;
@Test(priority=1)
public void NewRegisteringUser() throws InterruptedException {
//class new object
lanuchApp =new LaunchCloseApp(); //Instantiate class //as we are not in a static void method
//calling the launchCloseApp class to use it properties
//not repeating codes
lanuchApp.launchApp();
lanuchApp.driver.get("http://automationpractice.com/index.php"); lanuchApp.driver.findElement(By.linkText("Sign in")).click();
lanuchApp.driver.findElement(By.id("email_create")).sendKeys("gorgeous12@hotmail.com");
//lanuchApp.driver.findElement(By.xpath("/*[@id=\'create-account_form\']/div/div[3]/input[2]")).click();
lanuchApp.driver.findElement(By.name("SubmitCreate")).click();
//lanuchApp.driver.get("http://automationpractice.com/index.php?controller=authentication&back=my-account#account-creation");
//next page to complete new users details
Thread.sleep(3000);
lanuchApp.driver.findElement(By.id("uniform-id_gender2")).click();
lanuchApp.driver.findElement(By.id("customer_firstname")).sendKeys("Jane");
lanuchApp.driver.findElement(By.id("customer_lastname")).sendKeys("White");
//lanuchApp.driver.findElement(By.id("email")).sendKeys("gorgeous12@hotmail.com");
lanuchApp.driver.findElement(By.id("passwd")).sendKeys("mypwd123");
//dropdown for DOB
lanuchApp.driver.findElement(By.id("uniform-days")).click();
Select dropdownunDays= new Select (lanuchApp.driver.findElement(By.id("days")));
dropdownunDays.selectByIndex(1);
lanuchApp.driver.findElement(By.id("uniform-months")).click();
Select dropdownunMonths= new Select (lanuchApp.driver.findElement(By.id("months")));
dropdownunMonths.selectByVisibleText("March ");
lanuchApp.driver.findElement(By.id("uniform-years")).click();
Select dropdownunYears= new Select (lanuchApp.driver.findElement(By.id("years")));
dropdownunYears.selectByValue("1986");
lanuchApp.driver.findElement(By.id("uniform-newsletter")).click();
lanuchApp.driver.findElement(By.id("uniform-optin")).click();
lanuchApp.driver.findElement(By.id("firstname")).sendKeys("Jane");
lanuchApp.driver.findElement(By.id("lastname")).sendKeys("White");
lanuchApp.driver.findElement(By.id("company")).sendKeys("mycompany");
lanuchApp.driver.findElement(By.id("address1")).sendKeys("12");
lanuchApp.driver.findElement(By.id("address2")).sendKeys("The Blue Ave");
lanuchApp.driver.findElement(By.id("city")).sendKeys("Manhattan");
lanuchApp.driver.findElement(By.id("uniform-id_state")).click();
Select dropdownunState= new Select (lanuchApp.driver.findElement(By.id("id_state")));
dropdownunState.selectByVisibleText("New York");
lanuchApp.driver.findElement(By.id("postcode")).sendKeys("10022");
lanuchApp.driver.findElement(By.id("uniform-id_country")).click();
Select dropdownunCountry= new Select (lanuchApp.driver.findElement(By.id("id_country")));
dropdownunCountry.selectByValue("21");
lanuchApp.driver.findElement(By.id("phone")).sendKeys("0051636687932");
lanuchApp.driver.findElement(By.id("phone_mobile")).sendKeys("0051674687902");
lanuchApp.driver.findElement(By.id("alias")).sendKeys("gorgeous65@msn.com");
lanuchApp.driver.findElement(By.name("submitAccount")).click();
}

}


Click on any of the above class and run.


We separated the lauchApp and Closing App section from the rest of the code to avoid repeated codes.  We can call the code at anytime from another other class.


[TestNG] Running:
  /private/var/folders/w_/c4qj2zfx4d1_b44l10d_m9l40000gn/T/testng-eclipse--2128089963/testng-customsuite.xml

Starting ChromeDriver 2.32.498537 (cb2f855cbc7b82e20387eaf9a43f6b99b6105061) on port 44797
Only local connections are allowed.
Jan 26, 2018 12:25:56 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
browser has launched
PASSED: NewRegisteringUser

===============================================
    Default test
    Tests run: 1, Failures: 0, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================

[TestNG] Time taken by org.testng.reporters.XMLReporter@790da477: 6 ms
[TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 0 ms
[TestNG] Time taken by org.testng.reporters.jq.Main@1807f5a7: 31 ms
[TestNG] Time taken by org.testng.reporters.EmailableReporter2@626abbd0: 3 ms
[TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter@3aa078fd: 10 ms
[TestNG] Time taken by org.testng.reporters.JUnitReportReporter@2d0399f4: 3 ms



———————————————————

——————————-----------------------------------


Actual result can also be captured from the webDriver.

Comments

Popular posts from this blog

Date Picker 2 -Calendar

Working on date picking- part 2 package com.phpTravels.PHPTravels; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; public class LaunchClose { WebDriver driver ; @BeforeClass public void launchApp() { //instantiate Chrome Browser driver System.setProperty( "webdriver.chrome.driver" , "/Users/tester/Documents/webDrivers/chrome/chromedriver" ); driver = new ChromeDriver(); driver .get( "https://www.phptravels.net/" ); //boolean applaunch =driver.getCurrentUrl(); System. out .println( "browser has launched" ); } @AfterClass public void CloseApp() { driver .quit(); System. out .println( "browser has quite" ); } } Second class - package com.phpTravels.PHPTravels; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.D...

Cypress UI Automation - part 1

Java to Cypress-JavaScript Automation Migration Introduction: Across Cancer Research UK engineering department, we currently use a Java automation framework for our User interface and API testing. The framework has evolved in the last 3 to 4 years and currently, we have 19 products/project, (running approximately 1350 test scenarios) using the framework to run the respective sanity/regression packs. The project/products extend across different technology stacks such as Drupal, Symphony, React JS, .Net, OBI and Siebel CRM.   The Quality Assurance (QA) test team are currently under the process to be transformed into a fully-fledged Quality Assurance function.   As part of this transformation, we would like to have a comprehensive, automated test suite that can be maintained by developers and testers. Furthermore, our front-end web development is moving into JavaScript, now is the right time to migrate our automation framework also from java into JavaScript. The introduction ...