There no select class to handle dropdowns in Protractor, we have to use other methods only to select dropdowns
Dropdowns are one of the general elements present in any webpage after buttons and textbars dropdowns are the more frequently available element.
How to select a value in a dropdown in protractor:
80% of the dropdowns are single value dropdowns, single value dropdowns are normal dropdowns on which you can select only one value, once you choose a value, the dropdown shrinks.
browser.get("https://chercher.tech/practice/dropdowns")// find click the dropdown
element(by.tagName("select#first")).click();// click the option which has value='Yahoo'
element(by.css("#first [value='Yahoo']")).click();Complete program for handling dropdown in protractor.
import { browser, element, by} from 'protractor'
describe('Protractor Typescript Demo', function() {
browser.ignoreSynchronization = true; // for non-angular websites
it('Excel File Operations', function() {
// set implicit time to 30 seconds
browser.manage().timeouts().implicitlyWait(30000);
browser.get("https://chercher.tech/practice/dropdowns")
// find click the dropdown
element(by.tagName("select#first")).click();
// add sleep to give a time for te options to reveal
browser.sleep(1000)
// click the option which has value='Yahoo'
element(by.css("#first [value='Yahoo']")).click();
});
});
If you are looking only just to handle dropdown then the above one is the answer and that is enough for day-to-day work, so no need to read the below topics.
But if you are a person who wants to learn things for future purposes, then the below topics are for you.
Select class is nothing but a class name; you can even say it as a Dropdown class or IDonotCare class.
The reason why I say it as Select class is, in selenium webdriver(Java) we have a class called Select class which handles the standard dropdowns, so I going to name my class to have the standard.
If you name the class as like in other languages, people would understand the usage of it based on the name itself.
The Select class handles the standard single dropdowns and multiple dropdowns with methods present in the Select class. In the below topics, we would be exploring how to create methods to handle the single value dropdowns.
Methods Present in Select Class ( In our Select class )
Select by the index is nothing but selecting an option present in the dropdown based on their position in the dropdown.
In Java, the position starts from 0, but as we are creating our own method, so it is up to us to give the index, but I would suggest sticking with the standard. So we also will be starting the index from 0.
Please do not refer to the UI of the dropdown; we just need to use HTML source code only, not the UI.
For this purpose we have to create a Typescript class called Select in Select.ts file, Please do forgive if you feel like why does he explaining from very basic.
export class Select {// dropdown
dropdown:ElementFinder;//constructor accepts dropdown as element
constructor(dropdownElement:ElementFinder) {// assign block variable to the global variable
this.dropdown = dropdownElement;// click the dropdown
dropdownElement.click()public selectByIndex(index:number){index = index + 1;// select the option
this.dropdown.element(by.css("option:nth-child("+index+")")).click()// select the option
this.dropdown.element(by.xpath("//option["+index+"]")).click()//select.ts
import {browser, by, ElementFinder } from "protractor";
export class Select {
// dropdown
dropdown:ElementFinder;
//constructor accepts dropdown as element
constructor(dropdownElement:ElementFinder) {
// assign block variable to the global variable
this.dropdown = dropdownElement;
// click the dropdown
dropdownElement.click()
browser.sleep(1000)
}
public selectByIndex(index:number){
index = index + 1;
console.log("Selecting element based index : "+index)
// select the option
this.dropdown.element(by.css("option:nth-child("+index+")")).click()
}
}
Why do we need a Constructor in this case:
We have created our Select class now lest create the test cases using describe & it block using Jasmine.
import { browser, element, by, ElementFinder} from 'protractor'
import { Select } from './Select';
describe('Protractor Typescript Demo', function() {
browser.ignoreSynchronization = true; // for non-angular websites
it('Excel File Operations', function() {
// set implicit time to 30 seconds
browser.manage().timeouts().implicitlyWait(30000);
browser.get("https://chercher.tech/practice/dropdowns")
let dropdownEle:ElementFinder = element(by.css("select#first"))
let sel:Select = new Select(dropdownEle);
//select the value based on the index
sel.selectByIndex(3)
});
});selectByValue() method works based on an attribute called value
Almost all the methods are just the trick of CSS selector and XPath, in those methods we will be forming the CSS sector which will find the option based on the value attribute
If we can a method based on the attribute, don't you think we can form the methods based on the other attributes like id, class, name, so on.
We will be using the same class, but below will add code to the select class.
import {browser, by, ElementFinder } from "protractor";
export class Select {
public selectByValue(value:string){
console.log("Selecting element based value : "+value)
// select the option
this.dropdown.element(by.css("option[value='"+value+"']")).click()
}
// constructor and remaining code
}selectByText() method works based on the text present in the element
In this method, we will be finding the inner element based on the text present in the element.
public selectByVisibleText(visibleText:string){
console.log("Selecting element based text : "+visibleText)
// select the option
this.dropdown.element(by.xpath("//option[text()='"+visibleText+"']")).click()
}getOption method will return all the options present in the dropdown, including default option.
In this method, we will find all the element which has the option as their tag name.
public getOptions(visibleText:string){
console.log("returning all options : "+visibleText)
// return all the options
this.dropdown.all(by.css("option"))
}isMultiple method gives information about the dropdown, whether it is a single value dropdown or multiple value dropdown.
public isMultiple(visibleText:string){
console.log("returning all options : "+visibleText)
// select the option
this.dropdown.getAttribute("multiple").then(function(multipleOrNot){
if(multipleOrNot){
return true
}else{
return false;
}
})
}Below is the complete select class that we have built so far, these solely depend on my knowledge only so this is not a standard, but it is one of the ways.
import {browser, by, ElementFinder } from "protractor";
export class Select {
// dropdown
dropdown:ElementFinder;
//constructor accepts dropdown as element
constructor(dropdownElement:ElementFinder) {
this.dropdown = dropdownElement;
// click the dropdown
dropdownElement.click()
browser.sleep(1000)
}
public isMultiple(visibleText:string){
console.log("returning all options : "+visibleText)
// select the option
this.dropdown.getAttribute("multiple").then(function(multipleOrNot){
if(multipleOrNot){
return true
}else{
return false;
}
})
}
public getOptions(visibleText:string){
console.log("returning all options : "+visibleText)
// select the option
this.dropdown.all(by.css("option"))
}
public selectByVisibleText(visibleText:string){
console.log("Selecting element based text : "+visibleText)
// select the option
this.dropdown.element(by.xpath("//option[text()='"+visibleText+"']")).click()
}
public selectByValue(value:string){
console.log("Selecting element based value : "+value)
// select the option
this.dropdown.element(by.css("option[value='"+value+"']")).click()
}
public selectByIndex(index:number){
index = index+1;
console.log("Selecting element based index : "+index)
// select the option
this.dropdown.element(by.css("option:nth-child("+index+")")).click()
}
}We can use the sendKeys method to set the value in the dropdown/combo box; we have to provide exact text to be selected in the send keys method.
import { browser, element, by} from 'protractor'
describe('Protractor Typescript Demo', function() {
browser.ignoreSynchronization = true; // for non-angular websites
it('Excel File Operations', function() {
// set implicit time to 30 seconds
browser.manage().timeouts().implicitlyWait(30000);
browser.get("https://chercher.tech/practice/dropdowns")
// set the value using sendkeys
element(by.tagName("select#first")).sendKeys("Bing");
});
});Sometimes you might have come across situations where you have to check whether a particular value in the dropdown is selected or not.
isSelected() helps the user to see whether a value is selected or not in the dropdown, if the value is selected then it will return true otherwise false.
I would recommend you to use the XPath/CSS selector, which related to the dropdown and option like below.
import { browser, element, by} from 'protractor'
describe('Protractor Typescript Demo', function() {
browser.ignoreSynchronization = true; // for non-angular websites
it('Excel File Operations', function() {
// set implicit time to 30 seconds
browser.manage().timeouts().implicitlyWait(30000);
browser.get("https://chercher.tech/practice/dropdowns")
// Check value in dropdown selected or not
expect(element(by.tagName("select#first option[value='Bing']"))).toBe(true, "Bing is not selected in dropdown")
});
});Selecting a value from the dropdown is an easier task, but sometimes we may need to select the option without using Select class.
We can select the dropdown values in a few ways:
We can use the Javascript method to select an option from the dropdown with the help of JavaScriptExecutor. We have to set the dropdown option using the value property of the element.
1. Open the browser and navigate to URL
2. Find the dropdown
3. Set the value using Javascript executor
import { browser, element, by, ElementFinder} from 'protractor'
describe('Protractor Typescript Demo', function() {
browser.ignoreSynchronization = true; // for non-angular websites
it('Excel File Operations', function() {
// set implicit time to 30 seconds
browser.manage().timeouts().implicitlyWait(30000);
browser.get("https://chercher.tech/practice/dropdowns")
element(by.css("select#first")).then(function(element){
browser.executeScript("arguments[0].value='Yahoo'", element);
})
});
});I am Pavankumar, Having 8.5 years of experience currently working in Video/Live Analytics project.
Hi Pavan, I wanted compare dropdown values with expected list. Please tell me how to compare two list (comparing actual and expected list) Thanks, Mallik
hi Karthi, I have installed protractor and jasmine and related software but i am getting the below error when i am running the below comment. protractor conf.js Error is: DEPRECATION: Setting randomizeTests directly is deprecated, please use the random option in `configure` DEPRECATION: Setting specFilter directly on Env is deprecated, please use the specFilter option in `configure` Error: Error while waiting for Protractor to sync with the page: "both angularJS testability and angular testability are undefined
++ Below are my conf.js files
conf.js:
// An example configuration file.
exports.config = {
directConnect: true,
// Capabilities to be passed to the webdriver instance.
capabilities: {
'browserName': 'firefox',
SELENIUM_PROMISE_MANAGER: false
},
// Framework to use. Jasmine is recommended.
framework: 'jasmine',
// Spec patterns are relative to the current working directory when
// protractor is called.
specs: ['spec.js'],
// Options to be passed to Jasmine.
jasmineNodeOpts: {
defaultTimeoutInterval: 30000,
// SELENIUM_PROMISE_MANAGER : true,
}
};
Could you please help me on this.
How to handle a dropdown which do not shrinks after selecting a value in protractor?
Hi Ritika, I did not understand your question clearly but this might help you. When the dropdown is not shrinking, it may be because of your design( expected) or because dropdown taking a moment to respond. In such cases, try to find an area in your application, clicking there has no affect on page. Once you spot such spot, then after selecting dropdown please click there now dropdown should shrink
Hi, If the background page is not clickable, then how do i handle it? I am meaning to say, when the drop down opens, the modal window behind becomes invisible overlay, which goes to the background. please help