我在Selenium Webdriver中运行下面显示的代码。我正在创建类SalesForceApplicationMethod()
的{{1}}对象,然后访问.validation()
方法。代码进入方法(.validation()
),但它给出了以下错误:
错误追溯:
线程中的异常" main"来自org.openqa.selenium.support.ui.FluentWait。(FluentWait.java:102)的com.google.common.base.Preconditions.checkNotNull(Preconditi ons.java:212)的java.lang.NullPointerException .openqa.selenium.support.ui.WebDriverWait。(WebDrive rWait.java:71)at com.ynn.aratver。 .validation(Sa lesForceApplicationM ethod.java:27)at com.syntel.pratice.SalesForceApplicationMethod.main(SalesFor ceApplicationMethod. java:88)
我的代码:
package com.syntel.pratice;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class SalesForceApplicationMethod
{
WebDriver driver;
public void validation()
{
// App launcher clicking
WebDriverWait wait = new WebDriverWait(driver,25);
WebElement ele = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//span[@class='label slds-truncate slds-text-link']")));
ele.click();
// New button
WebElement newBtn1 = driver.findElement(By.xpath("//div[contains(text(),'New')]"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", newBtn1);
// Account name - first account
driver.findElement(By.xpath("//input[@class='input uiInput uiInputText
uiInput--default uiInput--input']")).sendKeys("srieedherks");
// Click Save
driver.findElement(By.xpath("//button[@class='slds-button slds-button--neutral uiButton--default uiButton--brand uiButton forceActionButton']")).click();
// Clicking new contact button
WebDriverWait wait1 = new WebDriverWait(driver, 20);
WebElement newbt = driver.findElement(By.xpath("//div[contains(text(),'New
Contact')]"));
JavascriptExecutor exe = (JavascriptExecutor)driver;
exe.executeScript("arguments[0].click();", newbt);
WebElement ele1 = driver.findElement(By.linkText("--None--"));
ele1.sendKeys("Mr.");
System.out.println("Selecting ");
// Entering first name
driver.findElement(By.xpath("//input[@class='compoundBorderBottom form-
element__row input']")).sendKeys("srieedher");
// Entering last name
driver.findElement(By.xpath("//input[@class='compoundBLRadius
compoundBRRadius form-element__row input']")).sendKeys("santhakumar");
// Click Save
driver.findElement(By.xpath("//button[@class='slds-button slds-button--
brand cuf-publisherShareButton undefined uiButton']")).click();
// Logout view profile
driver.findElement(By.xpath("//img[@src='https://c.ap5.content.force.com/pro
filephoto/005/T/1']")).click();
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("arguments[0].click();", newbt);
// Click log out
driver.findElement(By.xpath("//a[contains(@class, 'profile-link-label
logout uiOutputURL')]")).click();
}
public static void main(String[] args)
{
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver", "D:\\chromedriver.exe");
ChromeOptions o = new ChromeOptions();
o.addArguments("disable-extensions");
o.addArguments("--start-maximized");
WebDriver driver = new ChromeDriver(o);
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.get("https://login.salesforce.com/");
driver.findElement(By.id("username")).sendKeys("srieedher@force.com");
driver.findElement(By.id("password")).sendKeys("Sriee678$");
driver.findElement(By.id("Login")).click();
Scanner s = new Scanner(System.in);
System.out.println("Enter your otp:");
String i = s.next().toString();
System.out.println("the OTP is : "+i);
driver.findElement(By.id("smc")).sendKeys(i);
driver.findElement(By.id("save")).click();
driver.findElement(By.xpath("//div[@class='slds-icon-Waffle']")).click();
SalesForceApplicationMethod m1 = new SalesForceApplicationMethod();
m1.validation();
}
}
答案 0 :(得分:1)
因为SalesForceApplicationMethod
类中的驱动程序未初始化。在该类中添加以下构造函数。
public SalesForceApplicationMethod(WebDriver ldriver)
{
this.driver=ldriver;
}
在主课程中创建此课程的实例时,请传递WebDriver
。
SalesForceApplicationMethod m1 = new SalesForceApplicationMethod(driver);
希望这会对你有所帮助。感谢。
答案 1 :(得分:0)
您已创建了一个全局变量 WebDriver驱动程序; 然后在函数main()中写了
WebDriver driver = new ChromeDriver(o);
这将是main()函数块的局部变量。可以做的一件简单的事情是初始化全局变量,而不是创建一个同名的新局部变量。为此,只需用
替换main()块中的上述代码行 driver = new ChromeDriver(o);
这将初始化在类开头声明的全局变量。