RE: Using testNG Framework

Answered

This code works but now that I’m using  TestNG it doesn’t.  it seems that only the code in the @BeforeMethod works. as if nothing is passed the @Test. If I put all the code in the @BeforeMethod it runs.

 


package FrameWork;
import java.util.concurrent.TimeUnit;

import org.testng.annotations.Test;

import org.testng.annotations.BeforeMethod;

import org.testng.annotations.AfterMethod;

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.support.ui.Select;

import org.testng.annotations.Test;
public class NewTest {

public WebDriver driver;
@Test
publicvoid main() {
//Setting Variables
String strUsername = "name";

String strPassword = "PW";

String strAccountname = "new";
WebElement txtTwtelecomLogin = driver.findElement(By.className("apbutton"));
txtTwtelecomLogin.click();
WebElement txtUsername = driver.findElement(By.name("tbUserName"));
WebElement txtPassword = driver.findElement(By.name("tbPassword"));
WebElement txtLogin = driver.findElement(By.name("btnLogin"));
txtUsername.sendKeys(strUsername);
txtPassword.sendKeys(strPassword);
txtLogin.click();
}
@BeforeMethod
publicvoid beforeMethod() {
System.setProperty("webdriver.chrome.driver","C:/drivers/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
//Navigate to Test A
driver.get("https://twtelecom--testa.cs24.my.page.com/");
}
@AfterMethod
publicvoid afterMethod() {
driver.quit();
}
}

Richard Allen Train Asked on September 30, 2014 in Selenium WebDriver.
Add Comment
4 Answers

Hello sachin,

your codes works but i tried your code structure in Gmail

Here is the following code

public class NewTest {
public WebDriver driver;
@Test
public void main() {
        //Setting Variables
        // I gave wrong username and password in this scenario
        String strUsername = “name”;
        String strPassword = “PW”;
        WebElement txtUsername = driver.findElement(By.id(“Email”));
        txtUsername.sendKeys(strUsername);
        WebElement txtPassword = driver.findElement(By.id(“Passwd”));
        txtPassword.sendKeys(strPassword);
        WebElement txtLogin = driver.findElement(By.id(“.save”));
        txtLogin.click();
        }
@BeforeMethod
        public void beforeMethod() {
        System.setProperty(“webdriver.chrome.driver”,”D:/Selenium/Drivers/chromedriver.exe”);
        driver = new ChromeDriver(); //Chanhe is in this line (Removed WebDriver).
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        //Navigate to Test A
        driver.get(“https://gmail.com”);
        }
@AfterMethod
        public void afterMethod() {
        driver.quit();
        }
}

Now I want to write a another @ Test method and want to pass a correct username and password and then login without opening a new instance of chrome(I want to continue to write the corrected data in the same chrome instance).

How to proceed now?? 

Train Answered on October 1, 2014.

Som,

Could you please post your code correctly because I was unable to understand. Please do not insert code, just post your code. Also If code (My code above) then let it be and post your code with new question. Me or other members in this group will look into your query.

on October 1, 2014.

See this link: http://www.toolsqa.com/selenium-webdriver/testng-parameters-data-provider/ and let us know 🙂 .

on October 1, 2014.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.