I am creating page object model plus data driver framework.
I am writing Testcase for logIn but getting pagefactory nullpointerexception.
1. How I can initialise driver in order to avoid this error in testbase and call initialize method in AppTest.java?
2. Again How I can write code to call Sccreenshotpage class in my test script i have given code below for Sccreenshotpage class.
FAILED: Log("s@gmail.com", "sw45") java.lang.NullPointerException at org.openqa.selenium.support.pagefactory.DefaultElementLocator.findElement(DefaultElementLocator.java:69)
TestBase.java class
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class TestBase {
public WebDriver driver;
public void initialize() throws InterruptedException, IOException {
System.out.println("Launching browser");
System.setProperty("webdriver.chrome.driver",
"C:\\Users\\data\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.navigate().to("http://automationpractice.com/index.php?controller=authentication&back=my-account");
Thread.sleep(6000);
}
}
Excelreader.java to read data from excel file
import java.io.IOException;
import org.testng.annotations.DataProvider;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import selenium.org.sample.SampleProject.TestBase.TestBase;
public class ExcelReader extends TestBase {
@DataProvider(name = "testdata")
public Object[][] readexcel() throws IOException, BiffException {
File f = new File(
"C:\\Users\\Data_driven5.xls");
Workbook w = Workbook.getWorkbook(f);
Sheet s = w.getSheet("Sheet1");
int row = s.getRows();
int colums = s.getColumns();
String inputdata[][] = new String[row][colums];
for (int i = 0; i < row; i++) {
for (int j = 0; j < colums; j++) {
Cell c = s.getCell(j, i);
inputdata[i][j] = c.getContents();
System.out.println(inputdata[i][j]);
}
}
return inputdata;
}
}
Signin.java This this when I am finding element
public class SignIn extends TestBase {
@FindBy(xpath = "//*[@id=\"email\"]")
WebElement email_;
@FindBy(xpath = "//*[@id=\"passwd\"]")
WebElement pwd_;
@FindBy(xpath = "//*[@id=\"SubmitLogin\"]/span")
WebElement Sign_In;
public void setUserName(String email_1) {
email_.sendKeys(email_1);
}
public void setPwd(String pwd_1) {
pwd_.sendKeys(pwd_1);
}
public void Sign_In_btn() {
Sign_In.click();
}
}
AppTest.java Testcase for log in
public class AppTest extends ExcelReader {
TestBase TB = new TestBase();
@BeforeTest
void browserlaunch() throws InterruptedException, IOException
{
TB.initialize();
}
@Test(dataProvider = "testdata")
public void LogIn(String email, String pwd) throws IOException, InterruptedException {
System.out.println("Sign in page1");
SignIn loginpage = PageFactory.initElements(driver, SignIn.class);
loginpage.setUserName(email);
loginpage.setPwd(pwd);
loginpage.Sign_In_btn();
driver.manage().window().maximize();
try {
Assert.assertEquals(driver.getTitle(), "My account - My Store");
System.out.println("Log IN successfull1");
} catch (AssertionError E) {
System.out.println("Log IN un-successfull" + E);
}
Thread.sleep(8000);
System.out.println("after click");
}
}
ScreenshotPage.java
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import ru.yandex.qatools.ashot.AShot;
import ru.yandex.qatools.ashot.Screenshot;
import ru.yandex.qatools.ashot.shooting.ShootingStrategies;
import selenium.org.sample.SampleProject.TestBase.TestBase;
public class ScreenshotPage extends TestBase {
private WebDriver driver = new ChromeDriver();
public void ScreenshotPage1() throws InterruptedException, IOException {
Screenshot fpScreenshot = new AShot().shootingStrategy(ShootingStrategies.viewportPasting(1000))
.takeScreenshot(driver);
ImageIO.write(fpScreenshot.getImage(), "PNG", new File("D:/selenium/" + System.currentTimeMillis() + ".png"));
}
}
What I have tried:
I have tried to execute code above... just I want to know how I can call methods from other class to avoid nullpoinerexception because of driver. and create tastbase class