0

My code:

public class NewTest {
    public static WebDriver driver; 

@BeforeSuite(alwaysRun=true)
public void intiate() {
          System.setProperty("webdriver.chrome.driver", "C:\\selenium\\chromedriver.exe");
          WebDriver driver = new ChromeDriver();
          String Appurl= "http://toolsqa.com/automation-practice-form/";
          driver. manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS );
          driver.navigate().to(Appurl);

      } 


  @Test  (alwaysRun=true)
  public void Enterusernamedetails() {  
/*    System.setProperty("webdriver.chrome.driver", "C:\\selenium\\chromedriver.exe");
      WebDriver driver = new ChromeDriver();
      String Appurl= "http://toolsqa.com/automation-practice-form/";
      driver. manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS );
      driver.navigate().to(Appurl);*/

      WebElement UsrName = driver.findElement(By.name("firstname"));
      if(UsrName!=null)
      {
          UsrName.sendKeys("Hemamalini");
          System.out.println("First name has been entered");
      }
      else
      {
          System.out.println("First name element is not visible");
      }


  WebElement lastName = driver.findElement(By.name("lastname"));
  if(lastName!=null)
  {
      lastName.sendKeys("K");
      System.out.println("Last name has been entered");
  }
  else
  {
      System.out.println("Last name element is not visible");
  }

  WebElement radbtnSex = driver.findElement(By.id("sex-0"));
  if(radbtnSex!=null)
  {
      radbtnSex.click();
      System.out.println("Sex radio button has been clicked");
  }
  else
  {
      System.out.println("Sex radio button element is not been clicked");
  }

  WebElement radbtnExper = driver.findElement(By.id("exp-2"));
  if(radbtnExper!=null)
  {
      radbtnExper.click();
      System.out.println("Experience radio button has been clicked");
  }
  else
  {
      System.out.println("Experience radio button element is not been clicked");
  }

  WebElement txtDatePicker = driver.findElement(By.id("datepicker"));
  if(txtDatePicker!=null)   
  {
      txtDatePicker.sendKeys("10/11/2017");
      System.out.println("Date has been entered");
  }
  else
  {
      System.out.println("Date element is not visible");
  }

  WebElement chkbox = driver.findElement(By.id("profession-1"));
  if(chkbox!=null)
  {
      chkbox.click();
      System.out.println("Profession checkbox has been clicked");
  }
  else
  {
      System.out.println("Profession checkbox element is not been clicked");
  }

  WebElement chkboxtool = driver.findElement(By.id("tool-2"));
  if(chkboxtool!=null)
  {
      chkboxtool.click();
      System.out.println("Automation tool checkbox has been clicked");
  }
  else
  {
      System.out.println("Automation tool element is not been clicked");
  }

  Select drpContinents = new Select(driver.findElement(By.id("continents")));
  if(drpContinents!=null)
  {
      drpContinents.selectByVisibleText("Australia");
      System.out.println("Automation tool checkbox has been clicked");

  }

  Select drpCommands = new Select(driver.findElement(By.id("selenium_commands")));
  if(drpCommands!=null)
  {
      drpCommands.selectByVisibleText("Switch Commands");
      System.out.println("command has been clicked");
  }

      WebElement button = driver.findElement(By.id("submit"));
      if(button!=null)
      {
          button.click();
          System.out.println("button has been clicked");
      }
      else
      {
          System.out.println("button element is not been clicked");
      }

      WebElement uploadfile = driver.findElement(By.id("photo"));
      if(uploadfile!=null)
      {
          uploadfile.sendKeys("C:\\Users\\391387.CTS\\Documents\\New folder\\Spritz_Selenium_Grid_Setup_v 2 0.docx");
          System.out.println("document has been uploaded");
      }
      else
      {
          System.out.println("button document has not been uploaded");
      }

      driver.quit();
  }

}
  • Add your exception and on which line it fails. – FDM May 26 '17 at 8:39
1

Problem is you have initiated WebDriver driver multiple times.

  1. public static WebDriver driver;
  2. In @BeforeSuite(alwaysRun=true) see WebDriver driver = new ChromeDriver();
  3. In @Test (alwaysRun=true) see WebDriver driver = new ChromeDriver();

Here you have initiate driver 3 times and As per TestNG, @BeforeSuites run first. Change your code as under :

  1. public static WebDriver driver = null;

  2. In @BeforeSuite(alwaysRun=true) : Replace below code with WebDriver driver = new ChromeDriver();

    //WebDriver driver = new ChromeDriver(); // Old line
    driver = new ChromeDriver(); // new line
    
  3. In @Test (alwaysRun=true) : Comment/remove below code

    //System.setProperty("webdriver.chrome.driver",  "C:\\selenium\\chromedriver.exe");
    //driver = new ChromeDriver();
    

Hope you understand it is Basic Java Code error. Let me know If you require more help.

0

In our project we are using separate class to choose driver for our test, you can try this:

public class SetupBrowser {

public static WebDriver SetupDriver(String browser) {
    WebDriver driver = null;
    if (browser.equalsIgnoreCase("FF")) {

            System.setProperty("webdriver.gecko.driver","C:\\geckodriver-v0.16.1-win64\\geckodriver.exe");
            DesiredCapabilities capabilitiesFF = DesiredCapabilities.firefox();
            capabilitiesFF.setCapability(FirefoxDriver.MARIONETTE, true);
            //capabilitiesFF.setCapability(FirefoxDriver.PROFILE, true);
            driver = new FirefoxDriver(capabilitiesFF);

    }
    else if (browser.equalsIgnoreCase("IE")) {

        System.setProperty("webdriver.ie.driver", "C:\\IEDriverServer.exe");

        // Setting IE to clean session
        DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();
        capabilities.setCapability(InternetExplorerDriver.IE_ENSURE_CLEAN_SESSION, true);

        driver = new InternetExplorerDriver(capabilities);

    }
    else {
           driver = new ChromeDriver();
           //////////
               }
    return driver;
}

We read type of browser from properties file.


Call/Write code whenever you require to open browser :

SetupBrowser.SetupDriver("FF");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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