0

Can someone tell me why Public class dropdown is not working, I mean program is not selecting value from static dropdown.

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

import java.util.concurrent.TimeUnit;

public class new_class
{
    public static WebDriver driver;
    public static void main(String[] args) {
        /**Sterownik Dla Firefox */
        System.setProperty("webdriver.gecko.driver", "C:\\geckodriver.exe");
        //System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
        driver = new FirefoxDriver();

        driver.get("http://automationpractice.com/index.php");
        driver.findElement(By.xpath("//a[@title='Log in to your customer account']")).click();
        driver.findElement(By.xpath("//input[@id= 'email_create']")).sendKeys("[email protected]");

        driver.findElement(By.xpath("//button[@id='SubmitCreate']//span")).click();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); // Wait for maximum 10 seconds
        driver.findElement(By.xpath("//input[@id='id_gender1']")).click();
        driver.findElement(By.id("customer_firstname")).sendKeys("Mario");
        driver.findElement(By.xpath("//input[@id='customer_lastname']")).sendKeys("Kowalski");
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); // Wait for maximum 10 seconds
    }

    public class dropdown {
        public void main (String[] args)
        {
            Select s = new Select(driver.findElement(By.xpath("//select[@id='days']")));

            s.selectByValue("2");
        }
    }
}
2
  • Could you share the error? Commented Jan 27, 2019 at 8:19
  • What is your code supposed to do step by step? You have an inner class with non-static main method. What is the idea behind this approach?
    – Alexey R.
    Commented Jan 28, 2019 at 9:52

1 Answer 1

1

It all depends on how you are running this file.

Observe that dropdown is an inner class. If you run the new_class.java solely, it will execute new_class.main method. To run dropdown.main, you have to call java packageName.new_class$dropdown.

However, if you want to run the new_class' main method and afterwards the dropdown main, you can just create separated files and instantiate an object.

On new_class.java:

public class new_class
{
    public static WebDriver driver;
    public static void main(String[] args) {
        /**Sterownik Dla Firefox */
        System.setProperty("webdriver.gecko.driver", "C:\\geckodriver.exe");
        //System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
        driver = new FirefoxDriver();

        driver.get("http://automationpractice.com/index.php");
        driver.findElement(By.xpath("//a[@title='Log in to your customer account']")).click();
        driver.findElement(By.xpath("//input[@id= 'email_create']")).sendKeys("[email protected]");

        driver.findElement(By.xpath("//button[@id='SubmitCreate']//span")).click();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); // Wait for maximum 10 seconds
        driver.findElement(By.xpath("//input[@id='id_gender1']")).click();
        driver.findElement(By.id("customer_firstname")).sendKeys("Mario");
        driver.findElement(By.xpath("//input[@id='customer_lastname']")).sendKeys("Kowalski");
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); // Wait for maximum 10 seconds

        // Selecting on dropdown
        new dropdown(driver).selectSecondDay();
    }
}

On dropdown.java:

public class dropdown {

    public void dropdown(WebDriver driver) {
        this.driver = driver;
    }

    public void selectSecondDay () {
        Select s = new Select(this.driver.findElement(By.xpath("//select[@id='days']")));
        s.selectByValue("2");
     }
}

OBS: The code is not aligned with several naming conventions of the Java language - I would suggest you to take a look at them and refactor your names.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.