Selenium Drop Down Selection In https://www.goibibo.com/hotels/ website - selenium

https://www.goibibo.com/hotels/ Image
I want to select one city using selenium, but its not working
I have tried with xpath, css selector, id, className.\
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Goibibo {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C:\\Selenium\\chromedriver_2\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
//Goto Url Goibibo.com
driver.get("https://www.goibibo.com/hotels/");
System.out.println("WWW.GOIBIBO.COM");
//Select Country India
driver.findElement(By.xpath("//input[#name='CountryType']")).click();
System.out.println("Selected Country India");
// Click on Search Bar
driver.findElement(By.id("downshift-1-input")).click();
System.out.println("Clicked On Search Bar");
//Select Mysore City
driver.findElement(By.xpath("//*[#id=\"downshift-1-menu\"]/div/ul/li[2]/img")).click();;
}
}

You can try with the below xpath :
//p[text()='Mysore']
or
//img[contains(#alt,'Mysore')]/following-sibling::div
in code :
System.out.println("Clicked On Search Bar");
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//p[text()='Mysore']"))).click();
System.out.println("Task has been done !");

Related

Automation Testing - Selenium WebDriver - Running Multiple Test Cases

I have got some problems with my automation testing.
I have nearly around 50 test cases in my Eclipse IDE. All test cases are in different classes. Also, I have got one BaseClass that contains #beforeclass and #afterclass. In #beforeclass, the browser opens, URL opens and website URL opens then it does login procedure.
Then my test cases work. All of them begin with #Test annotation.
I connect them with using TestNG suite.
Base Class:
My BaseClass.java class
MyBaseClass.java class
TestNg:
My Suite
Example of Test Case (Class):
My Example Test Case (Class)
Here is my question:
I want to use priority (like #Test (priority=1)) for these classes (test cases) for reducing the workforce. But when there is a problem with my codes; my automation test stops. I want to, it would continue besides the stop.
The second option is using TestNG. TestNG is ok but every case, the browser opens. How can I create a test like just opening one browser then run all test cases in that browser?
By the way here is my example test case for your imagine all picture:
-#beforeclass: open browser - open URL - login
#test1: go to products screen - click create a product - create a product with initial quantity - then click save button, it should be in the products screen again.
#test2: click create a product button again. - create a product without initial quantity - click save button, it should get an error. click the cancel button to continue the third #test
#test3: so on...
-#afterclass: close browser
I really appreciate for your help! Thank you!
Edit: I switched my codes as like this. Because I dont want to my automation opens a new browser over and over again. All my test cases relates with only one screen. (products) All I want is by ordering:
Initiate #BeforeSuite (open browser, URL..)
Initiate #BeforeClass(go to Products screen - because it is common screen for all cases)
Initiate #Test for Test Case 1
Initiate #AfterClass (go to Products screen again for a connection
with test case 2
Initiate #Test for Test Case 2
My Test NG class:
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="AllTests" verbose="10" >
<test name="prcr1.1" >
<classes>
<class name="com.example.product.prcr1dot1" />
</classes>
</test>
<test name="prcr1.2" >
<classes>
<class name="com.example.product.prcr1dot2" />
</classes>
</test>
</suite>
My Base Class:
public class BaseClass {
protected WebDriver driver;
#BeforeSuite
public void openMyexample() throws InterruptedException {
System.out.println("Initiate LoginTest Test...");
driver = utilities.DriverFactory.open("chrome");
driver.manage().window().maximize();
driver.get("https://mystage.example.com/en/public/login/?isTestAutomationLive=true");
System.out.println("example Page Has Been Opened..");
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.elementToBeClickable((By.name("email"))));
// Enter Username Section
WebElement username = driver.findElement(By.name("email"));
username.sendKeys("automationproducts4#example.com");
Thread.sleep(1000);
wait.until(ExpectedConditions.elementToBeClickable((By.name("password"))));
// Enter Password Section
WebElement password = driver.findElement(By.name("password"));
password.sendKeys("Test*01");
Thread.sleep(1000);
wait.until(ExpectedConditions.elementToBeClickable((By.xpath("//form[#id='frmLogin']/button"))));
// Click Login Button
WebElement logInButton = driver.findElement(By.xpath("//form[#id='frmLogin']/button"));
logInButton.click();
// PAGE LOADER
wait.until(ExpectedConditions.elementToBeClickable((By.xpath("//span[contains(.,'×')]"))));
Thread.sleep(1000);
// Integration Message Function WebElement - Option 1: WebElement
WebElement popupCancelButton = driver.findElement(By.xpath("//span[contains(.,'×')]"));
popupCancelButton.click();
Thread.sleep(3000);
}
#BeforeClass
public void openProducts() throws InterruptedException {
WebDriverWait wait = new WebDriverWait(driver, 20);
// From Dashboard Section to Product Section
wait.until(ExpectedConditions.elementToBeClickable((By.xpath("//a[contains(text(),'Products')]"))));
WebElement productButton = driver.findElement(By.xpath("//a[contains(text(),'Products')]"));
productButton.click();
Thread.sleep(4000);
wait.until(ExpectedConditions.elementToBeClickable((By.xpath("//button[contains(.,'Create Product')]"))));
WebElement createProductButton = driver.findElement(By.xpath("//button[contains(.,'Create Product')]"));
createProductButton.click();
Thread.sleep(4000);
}
#AfterClass
public void closeProducts() throws InterruptedException {
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.elementToBeClickable((By.xpath("//a[contains(text(),'Products')]"))));
WebElement productButton = driver.findElement(By.xpath("//a[contains(text(),'Products')]"));
productButton.click();
Thread.sleep(4000);
}
#AfterSuite
public void closeMyexample() throws InterruptedException {
WebDriverWait wait = new WebDriverWait(driver, 60);
Thread.sleep(4000);
// Sign Out Thread.sleep(5000);
wait.until(ExpectedConditions.elementToBeClickable((By.xpath("//div[2]/i"))));
WebElement logoutScrollDownButton = driver.findElement(By.xpath("//div[2]/i"));
logoutScrollDownButton.click();
Thread.sleep(3000);
WebElement signoutButton = driver.findElement(By.xpath("//a[contains(.,'Sign Out')]"));
signoutButton.click();
// Close Browser
System.out.println("Your Test Has Been Ended Successfully.");
Thread.sleep(1000);
System.out.println("Your Test is going to Close..");
driver.quit();
}
// Sleep Function
private void sleep(long m) {
try {
Thread.sleep(m);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
My Test Case 1:
package com.example.product;
import utilities.BaseClass;
//Test Case 1: PRCR - 1.1
//Creating a new product with a unique SKU
public class prcr1dot1 extends BaseClass {
#Test
public void prcr1dot1() throws InterruptedException {
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.elementToBeClickable((By.name("sku"))));
String uuid = UUID.randomUUID().toString();
driver.findElement(By.name("sku")).sendKeys(uuid);
driver.findElement(By.name("name")).sendKeys(uuid);
WebElement packType = driver
.findElement(By.xpath("//kendo-dropdownlist[#id='packTypeName']//span[#class='k-input']"));
packType.click();
wait.until(ExpectedConditions.elementToBeClickable((By.xpath(
"/html//platform-root/kendo-popup[#class='k-animation-container k-animation-container-shown']//input"))));
driver.findElement(By.xpath(
"/html//platform-root/kendo-popup[#class='k-animation-container k-animation-container-shown']//input"))
.sendKeys(uuid);
wait.until(ExpectedConditions.elementToBeClickable((By.id("btnCreateProductPackTypeName"))));
WebElement createPackType = driver.findElement(By.id("btnCreateProductPackTypeName"));
createPackType.click();
wait.until(ExpectedConditions.elementToBeClickable((By.xpath("//button[contains(.,'SAVE')]"))));
WebElement productSaveButton = driver.findElement(By.xpath("//button[contains(.,'SAVE')]"));
productSaveButton.click();
Thread.sleep(8000);
}
}
My Test Case 2:
import utilities.BaseClass;
public class prcr1dot2 extends BaseClass {
// Test Case 2: PRCR - 1.2
// Creating a new product with the used SKU
#Test
public void prcr1dot2() throws InterruptedException {
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.elementToBeClickable((By.name("sku"))));
driver.findElement(By.name("sku")).sendKeys("SKU08");
String uuid = UUID.randomUUID().toString();
driver.findElement(By.name("name")).sendKeys(uuid);
WebElement packType = driver
.findElement(By.xpath("//kendo-dropdownlist[#id='packTypeName']//span[#class='k-input']"));
packType.click();
wait.until(ExpectedConditions.elementToBeClickable((By.xpath(
"/html//platform-root/kendo-popup[#class='k-animation-container k-animation-container-shown']//input"))));
driver.findElement(By.xpath(
"/html//platform-root/kendo-popup[#class='k-animation-container k-animation-container-shown']//input"))
.sendKeys(uuid);
wait.until(ExpectedConditions.elementToBeClickable((By.id("btnCreateProductPackTypeName"))));
WebElement createPackType = driver.findElement(By.id("btnCreateProductPackTypeName"));
createPackType.click();
JavascriptExecutor jsx = (JavascriptExecutor) driver;
jsx.executeScript("window.scrollBy(0,450)", "");
WebElement productSaveButton = driver.findElement(By.xpath("//button[contains(.,'SAVE')]"));
productSaveButton.click();
Thread.sleep(5000);
wait.until(ExpectedConditions.elementToBeClickable((By.xpath("//button[contains(.,'Create Product')]"))));
WebElement createProductButton2 = driver.findElement(By.xpath("//button[contains(.,'Create Product')]"));
createProductButton2.click();
wait.until(ExpectedConditions.elementToBeClickable((By.name("sku"))));
driver.findElement(By.name("sku")).sendKeys("SKU08");
String uuid2 = UUID.randomUUID().toString();
driver.findElement(By.name("name")).sendKeys(uuid2);
WebElement packType2 = driver
.findElement(By.xpath("//kendo-dropdownlist[#id='packTypeName']//span[#class='k-input']"));
packType2.click();
wait.until(ExpectedConditions.elementToBeClickable((By.xpath(
"/html//platform-root/kendo-popup[#class='k-animation-container k-animation-container-shown']//input"))));
String uuid3 = UUID.randomUUID().toString();
driver.findElement(By.xpath(
"/html//platform-root/kendo-popup[#class='k-animation-container k-animation-container-shown']//input"))
.sendKeys(uuid3);
wait.until(ExpectedConditions.elementToBeClickable((By.id("btnCreateProductPackTypeName"))));
WebElement createPackType2 = driver.findElement(By.id("btnCreateProductPackTypeName"));
createPackType2.click();
/*
* WebElement productSaveButton2 =
* driver.findElement(By.xpath("//button[contains(.,'SAVE')]"));
* productSaveButton2.click(); Thread.sleep(5000); WebElement
* productCancelButton2 =
* driver.findElement(By.xpath("//button[contains(.,'CANCEL')]"));
* productCancelButton2.click(); Thread.sleep(2000);
* wait.until(ExpectedConditions.elementToBeClickable((By.xpath(
* "//button[contains(.,'YES')]")))); WebElement productCancelYesButton =
* driver.findElement(By.xpath("//button[contains(.,'YES')]"));
* productCancelYesButton.click();
*/
}
}
I am not sure it will solve your problem or not. But This idea works for me. Using this way you can run all the test case on same browser without opening multiple times.
Here I have three class
test1.java
test2.java
test2.java
And Mainclass.java
Here is test1.java
package test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
public class test1 {
#BeforeClass
public void before(){
}
#Test
public static void test(WebDriver driver){
driver.get("https://www.google.com");
}
#AfterMethod
public void after(){
}
}
Here is test2.java
package test;
import org.openqa.selenium.WebDriver;
import org.testng.annotations.Test;
public class test2 {
#Test
public static void test(WebDriver driver){
driver.get("https://www.yahoo.com");
}
}
Here is mainclass.java file where I passed driver session to the test
package test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Mainclass {
public static void main(String[]args){
WebDriver driver;
driver=new ChromeDriver();
test1.test(driver);
test2.test(driver);
}
}
Now you need to run mainclass from testing.xml file.
From the main class you can open browser one time and passed driver to your all the test case so it will use browser window to run the test case.
I am not sure any other ideal idea is available or not but it will surely work for you

I cannot select from city in MakeMyTrip wesite with Selenium WebDriver

I cannot select from city in MakeMyTrip wesbite with Selenium WebDriver. It does not select the specified city. It should click on the specified city and display the city in the "from city" field.
Here is my code:
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.Select;
public class Makemytrip {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "D:\\Selenium\\chromedriver_win32\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://www.makemytrip.com/");
String actualTitle=" ";
actualTitle=driver.getTitle();
String url=driver.getCurrentUrl();
System.out.println(url);
String expectedTitle=actualTitle;
if(actualTitle.contentEquals(expectedTitle)){
System.out.println("Test pass");
}
else{
System.out.println("Test fail");
}
WebElement roundtrip=driver.findElement(By.xpath(".//label[#class='label_text flight-trip-type']"));
roundtrip.click();
System.out.println("Select one way option");
//driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.findElement(By.xpath(".//div/section/div/div/input[#id='hp-widget__sfrom']")).click();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
List<WebElement> dd_menu=driver.findElements(By.xpath(".//ul[#id='ui-id-1']/li/div/p/span"));
for(int i=0;i<dd_menu.size();i++){
WebElement element=dd_menu.get(i);
String innerhtml=element.getAttribute("innerHTML");
if(innerhtml.contentEquals("Hyderabad, India HYD" )){
element.click();
break;
}
System.out.println("Values in list " +innerhtml);
}
}
}
Input field is kind of autosuggestive dropdown here...you should use sendKeys here...one more thing use explicit wait here...I've used Thread.sleep() here...you can use WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("someid"))); Modify your code with following snippet..e.g.
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "D:\\Selenium\\chromedriver_win32\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://www.makemytrip.com/");
String actualTitle=" ";
actualTitle=driver.getTitle();
String url=driver.getCurrentUrl();
System.out.println(url);
String expectedTitle=actualTitle;
if(actualTitle.contentEquals(expectedTitle)){
System.out.println("Test pass");
}
else{
System.out.println("Test fail");
}
WebElement roundtrip=driver.findElement(By.xpath(".//label[#class='label_text flight-trip-type']"));
roundtrip.click();
System.out.println("Select one way option");
WebElement we = driver.findElement(By.xpath(".//div/section/div/div/input[#id='hp-widget__sfrom']"));
we.clear();
Thread.sleep(3000);
we.sendKeys("hyde");
Thread.sleep(3000);
we.sendKeys(Keys.RETURN);
}
I was facing the same issue during testing. Whenever you try to open "makemytrip.com" site.
Click here to check whether you are getting the same window
Because of this menu, it block the entire WebElement to get access. To resolve this problem, follow the steps given below: -
Press Ctrl+Shift+i
Click on Inspect Pointer from the left top window of inspect tools.
Drag the pointer on window screen.
Select and copy the selected tagName's Xpath available on the inspect tool.
Here is the image of intermediate help for new learner
Paste it on your respective working IDE as shown below. Here is the working code to resolved the issue
Hope this will help you.
Please use the below code to solve the issue.
String driverPath = "C:\\geckodriver.exe";
System.setProperty("webdriver.gecko.driver", driverPath);
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("https://www.makemytrip.com/flights/");
Thread.sleep(2000);
Actions act = new Actions(driver);
WebElement ele= driver.findElement(By.xpath("//label[#for=\"fromCity\"]"));
act.doubleClick(ele).perform();

Need to select "Male" from the dropdown in gmail account creation page using selenium webdriver (I want to use SelectbyIndex method)

I tried below two codes. Both didn't select the "Male" option. Could anyone please let me know where I'm doing a mistake.
It's very difficult to post the code in this site. So many conditions
My code:
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;
public class webelements2 {
public static void main(String[] args) throws InterruptedException
{
System.setProperty("webdriver.gecko.driver","C:\\Users\\rpremala003\\Downloads\\geckodriver-v0.14.0-win64\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("https://accounts.google.com/SignUp?service=mail&continue=https%3A%2F%2Fmail.google.com%2Fmail%2F&ltmpl=default");
WebElement google = driver.findElement(By.xpath(".//*[#id='Gender']/div"));
google.click();
Select dropdown = new Select (driver.findElement(By.xpath(".//*[#id='Gender']/div")));
dropdown.selectByIndex(1);
}
}
Even I used sendkeys method. But it didn't work for me
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;
public class webelements2 {
public static void main(String[] args) throws InterruptedException
{
System.setProperty("webdriver.gecko.driver","C:\\Users\\rpremala003\\Downloads\\geckodriver-v0.14.0-win64\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("https://accounts.google.com/SignUp?service=mail&continue=https%3A%2F%2Fmail.google.com%2Fmail%2F&ltmpl=default");
WebElement google = driver.findElement(By.xpath(".//*[#id='Gender']/div"));
google.sendKeys("Male");
google.click();
}
Please suggest me how to overcome this problem
You can use Select() with <select>, <option> elements only. In this current case you can simply click() on drop-down and then click() to choose required option:
WebElement google = driver.findElement(By.xpath(".//*[#id='Gender']/div"));
google.click();
WebElement option = (driver.findElement(By.xpath("//div[text()='Male']"));
option.click();
You might also need to wait until option to be clickable:
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement option = wait.until(elementToBeClickable(By.xpath("//div[text()='Male']")));
option.click();
Simply use this and let me know if it works for you:
For Female
driver.findElement(By.id("Gender")).click();
driver.findElement(By.id(":e")).click();
For Male
driver.findElement(By.id("Gender")).click();
driver.findElement(By.id(":f")).click();
For Other
driver.findElement(By.id("Gender")).click();
driver.findElement(By.id(":g")).click();
For Rather not say
driver.findElement(By.id("Gender")).click();
driver.findElement(By.id(":h")).click();
And if you need for sign-up gmail, I tried Once:
driver.manage().window().maximize();
driver.get("https://accounts.google.com/SignUp?service=mail&continue=https%3A%2F%2Fmail.google.com%2Fmail%2F&ltmpl=default");
driver.findElement(By.xpath(".//*[#id='FirstName']")).sendKeys("Name");
driver.findElement(By.xpath(".//*[#id='LastName']")).sendKeys("Last name");
driver.findElement(By.xpath(".//*[#id='GmailAddress']")).sendKeys("Email id");
driver.findElement(By.xpath(".//*[#id='Passwd']")).sendKeys("Password");
driver.findElement(By.xpath(".//*[#id='PasswdAgain']")).sendKeys("Password Again");
//Input the month
List<WebElement> month_dropdown = driver.findElements(By.xpath(".//*[#id='BirthMonth']/div"));
//iterate the list and get the expected month
Thread.sleep(3000);
for (WebElement month_ele:month_dropdown){
String expected_month = month_ele.getAttribute("innerHTML");
// Break the loop if match found
Thread.sleep(3000);
if(expected_month.equalsIgnoreCase("August")){
month_ele.click();
break;
}
driver.findElement(By.id("BirthMonth")).click();
driver.findElement(By.id(":3")).click();
driver.findElement(By.xpath(".//*[#id='BirthDay']")).sendKeys("14");
driver.findElement(By.xpath(".//*[#id='BirthYear']")).sendKeys("1988");
driver.findElement(By.id("Gender")).click();
driver.findElement(By.id(":e")).click();
driver.findElement(By.xpath(".//*[#id='RecoveryPhoneNumber']")).sendKeys("4694222863");
driver.findElement(By.xpath(".//*[#id='RecoveryEmailAddress']")).sendKeys("recovery email id");
driver.findElement(By.id("submitbutton")).click();
Thread.sleep(3000L);

Controls not recognizing inside a SignIn form using Selenium WebDriver?

I'm trying to automate Sign In functionality in www.sears.com, but could not recognize the Email text field using the below code
package com.bigbasket.framework.lab;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class Sears {
public static void main(String[] args) {
WebDriver browser = new FirefoxDriver();
browser.get("http://www.sears.com/en_us.html");
browser.manage().window().maximize();
browser.findElement(By.xpath("//*[#id='gnf_01_tree_item_5']/span/a")).click();
WebElement currentElement = browser.findElement(By.xpath("//*[#id='myProfiles']/div"));
Actions actions = new Actions(browser);
actions.moveToElement(currentElement).build().perform();
browser.findElement(By.xpath("//*[#id='subnavDD_myProfile']/ul/li[1]/p/a")).click();
try{
WebElement formElement = browser.findElement(By.id("loginFormDisplay"));
currentElement = formElement.findElement(By.xpath("//*[#id='email']"));
}catch(Exception e){
System.out.println(e.getMessage());
}
WebDriverWait wait = new WebDriverWait(browser, 30);
wait.until(ExpectedConditions.visibilityOf(currentElement));
currentElement.sendKeys("srinimarva#gmail.com");
browser.quit();
}
}
Could you please help me in resolving this? Apologize I'm an amateur in Selenium WebDriver for asking a silly question.
your email field is in iframe so to access it you need to switch to frame first, following is the working code for your issue :
WebDriver browser = new FirefoxDriver();
browser.get("http://www.sears.com/en_us.html");
browser.findElement(By.xpath("//*[#id='gnf_01_tree_item_5']/span/a")).click();
WebElement currentElement = browser.findElement(By.xpath("//*[#id='myProfiles']/div"));
Actions actions = new Actions(browser);
actions.moveToElement(currentElement).build().perform();
WebDriverWait wait = new WebDriverWait(browser, 10);
wait.until(ExpectedConditions.visibilityOf(browser.findElement(By.xpath(".//*[#id='subnavDD_myProfile']/ul/li[1]/p/a"))));
browser.findElement(By.xpath(".//*[#id='subnavDD_myProfile']/ul/li[1]/p/a")).click();
try{
WebElement frameElement = browser.findElement(By.xpath(".//iframe[contains(#name, 'easyXDM_default')]"));
browser.switchTo().frame(frameElement);
}
catch(Exception e){
System.out.println(e.getMessage());
}
browser.findElement(By.name("email")).sendKeys("srinimarva#gmail.com");
browser.quit();

TestNG: Method requires 2 parameters but 0 were supplied in the #Test annotation

I am a newbie to selenium testing.
Basically,
I have a selenium class like :
import org.testng.annotations.Test;
import org.testng.annotations.Test;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
import org.testng.annotations.Test;
import com.google.common.base.Function;
import java.awt.AWTException;
import java.awt.Robot;
import org.openqa.selenium.By;
import org.openqa.selenium.Point;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.Test;
import org.openqa.selenium.firefox.FirefoxDriver;
public class SeleniumTests {
public void commonFunction(){
System.setProperty("webdriver.chrome.driver", "C://Downloads//chromedriver_win32//chromedriver.exe");
WebDriver driver = new ChromeDriver();
Actions builder = new Actions(driver);
driver.get("http://localhost:3000");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
test1(driver,builder);
}
//The driver,builder will be used by all the below 3 test functions.
/**
* Calling the Most Visited API -- TEST1
*/
public void test1(WebDriver driver,Actions builder){
WebElement most_visited_link = driver.findElement(By.id("mostVisited"));
Thread.sleep(2000);
Action mouseOvermost_visited_link = builder.moveToElement(most_visited_link).build();
mouseOvermost_visited_link.perform();
Thread.sleep(2000);
driver.findElement(By.id("mostVisited")).click();
Thread.sleep(1000);
WebElement most_Visited_button = driver.findElement(By.id("datePicked"));
Action mouse_most_VisitedOverDatePicker = builder.moveToElement(most_Visited_button).build();
mouse_most_VisitedOverDatePicker.perform();
Thread.sleep(2000);
driver.findElement(By.id("datePicked")).click();
Thread.sleep(2000);
driver.findElement(By.linkText("6")).click();
Thread.sleep(5000);
test2(driver,builder);
}
/**
* Calling the Status Pie API -- TEST2
* #param builder
* #param driver
*/
public void test2(WebDriver driver,Actions builder){
WebElement status_pie_link = driver.findElement(By.id("statusPie"));
Thread.sleep(2000);
//Actions builder = new Actions(driver);
Action mouseOverStatusPie = builder.moveToElement(status_pie_link).build();
mouseOverStatusPie.perform();
Thread.sleep(1000);
driver.findElement(By.id("statusPie")).click();
Thread.sleep(1000);
WebElement status_pie_button = driver.findElement(By.id("datePicked"));
Action mouseOver_Status_pie_DatePicker = builder.moveToElement(status_pie_button).build();
mouseOver_Status_pie_DatePicker.perform();
Thread.sleep(2000);
driver.findElement(By.id("datePicked")).click();
Thread.sleep(2000);
driver.findElement(By.linkText("6")).click();
Thread.sleep(5000);
test3(driver,builder);
}
/**
* Calling the Old Data API -- TEST3
*/
public void test2(WebDriver driver,Actions builder){
WebElement old_data_link = driver.findElement(By.id("oldData"));
Thread.sleep(2000);
// Actions builder = new Actions(driver);
Action mouseOverOldData = builder.moveToElement(old_data_link).build();
mouseOverOldData.perform();
Thread.sleep(2000);
driver.findElement(By.id("oldData")).click();
Thread.sleep(1000);
WebElement old_data_button = driver.findElement(By.id("datePicked"));
Action mouseOverDatePicker = builder.moveToElement(old_data_button).build();
mouseOverDatePicker.perform();
Thread.sleep(2000);
driver.findElement(By.id("datePicked")).click();
Thread.sleep(2000);
driver.findElement(By.linkText("6")).click();
Thread.sleep(5000);
driver.quit();
}
}
The code executes as expected on the browser and closes the chrome window.
However, in the index report, I see log as :
3 methods, 2 skipped, 1 passed
Skipped methods :
mostVisitedfunction
statuspiefunction
Passed methods :
createConnection
Any idea where am I going wrong ?
Thanks in advance.
The method mostVisitedfunction should not have annotation because it is the helping method for yoyr test case. Instead create a new class, create a object of the same in the #test class and then call that method..
See the below example..
My test is
//MAximize the Screen
driver.manage().window().maximize();
//Go to Gmail Login Page
SignInPage SignInPage = WebUtils.GoToSignInPage(driver);
//Sign in to Login page -Send Username
SignInPage.SendkeysMethodForSignInPAge(driver, By.cssSelector("input[id='Email']") , "kishanpatelllll.8#gmail.com" );
//Click on Next
SignInPage.ClickToLogin(driver, By.cssSelector("input[id='next']"));
//Wait for password field to be visible
SignInPage.WaitForElementTobeVisible(driver, By.cssSelector("input[id='Passwd'][type='password']"));
So when i call the method SendkeysMethodForSignInPAge i wont write it in #Test.
See SendkeysMethodForSignInPAge method:
public class SignInPage {
public void SendkeysMethodForSignInPAge(WebDriver driver, By by, String s) {
WebUtils.Sendkeys(driver,by,s);
}
I created a new class and there i defined it.
This is basic flow.
Hope you can relate this.
Reply to me, if your are still stuck.
Happy Learning :-)
The error comes because if you have parameters in your #Test annotated method, then those need to come from #Parameters or #DataProvider. Else, if none of the annotation supplies the arguments, which is your case, then the error is thrown.
Apart from that what #Kishan has suggested in terms of structure of code is correct. You need to differentiate between your tests and common functions.
#Test
public void assertBackToLogin(WebDriver driver) throws InterruptedException {
LoginPage login = new LoginPage (driver);
login.assertLogin();
}
Is NOT working
Remove all the parameters and run
#Test
public void assertBackToLogin() throws InterruptedException {
LoginPage login = new LoginPage (driver);
login.assertLogin();
}