package bannerTstNG;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class BannerTestNG {
@BeforeTest
public void OpenTheSuperAdmin() throws InterruptedException {
System.setProperty("webdriver.chrome.driver","D:\\myselenium\\bannerTstNG\\driver\\chromedriver\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://ss-superadmin-staging.labaiik.net/");
driver.manage().window().maximize();
driver.findElement(By.xpath("//input[@id='email']")).sendKeys("[email protected]");
driver.findElement(By.xpath("//input[@id='password']")).sendKeys("admin");
Thread.sleep(1000);
driver.findElement(By.xpath("//button[contains(text(),'Login')]")).click();
Thread.sleep(6000);
}
@Test
public void ClickOnBanner() {
driver.findElement(By.xpath("//body/div[2]/div[1]/div[1]/div[1]/div[3]/div[1]/ul[1]/li[4]/a[1]")).click
}
}
該函式OpenTheSuperAdmin()正在運行,但在ClickOnBanner()執行時,出現以下錯誤:driver cannot be resolved.
為什么OpenTheSuperAdmin()在沒有任何錯誤的情況下執行并且驅動程式錯誤沒有顯示在那里?
uj5u.com熱心網友回復:
您正在方法driver內部實體化變數,OpenTheSuperAdmin因此它不在ClickOnBanner測驗范圍內。請嘗試以下操作:
package bannerTstNG;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class BannerTestNG {
private WebDriver driver;
@BeforeTest
public void OpenTheSuperAdmin() throws InterruptedException {
System.setProperty("webdriver.chrome.driver","D:\\myselenium\\bannerTstNG\\driver\\chromedriver\\chromedriver.exe");
driver = new ChromeDriver();
driver.get("https://ss-superadmin-staging.labaiik.net/");
driver.manage().window().maximize();
driver.findElement(By.xpath("//input[@id='email']")).sendKeys("[email protected]");
driver.findElement(By.xpath("//input[@id='password']")).sendKeys("admin");
Thread.sleep(1000);
driver.findElement(By.xpath("//button[contains(text(),'Login')]")).click();
Thread.sleep(6000);
}
@Test
public void ClickOnBanner() {
driver.findElement(By.xpath("//body/div[2]/div[1]/div[1]/div[1]/div[3]/div[1]/ul[1]/li[4]/a[1]")).click
}
}
通過將驅動程式宣告為類中的一個欄位,您現在可以從類內的任何測驗中訪問它。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/339955.html
上一篇:如何在SeleniumWebdriverJava中定位沒有標簽名稱的元素
下一篇:PythonSelenium,即使元素是可互動的,輸入欄位也會回傳ElementNotInteractableException
