gpt4 book ai didi

java - 我不想在Java中重复代码selenium

转载 作者:行者123 更新时间:2023-12-01 09:25:39 29 4
gpt4 key购买 nike

我不想使用 TestNG 在每个测试用例中重复以下代码。请给个建议。我保留 propfis 作为全局变量,在 openBrowser() 中初始化它们,然后在其他方法中使用,但我得到 Prop 上的 NullPointerException。如果我添加下面的行,那么我就不会明白。

我只想加载一次属性文件并重新使用它。

Properties prop = new Properties();
FileInputStream fis = new
FileInputStream("C:\\Users\\xxxxx\\URL.properties");
prop.load(fis);

代码:

public class CallUrl {  
WebDriver driver;
Properties prop ;
FileInputStream fis;
@BeforeTest
public void openBrowser() throws IOException
{
Properties prop = new Properties();
FileInputStream fis = new FileInputStream("C:\\Users\\xxxx\\URL.properties");
prop.load(fis); System.setProperty("webdriver.chrome.driver","C:\\Users\\xxxx\\chromedriver.exe");
String browserType = prop.getProperty("Browser");
if ( browserType.equals("Chrome"))
{ System.setProperty("webdriver.chrome.driver","C:\\Users\\Ashish\\Documents\\Selenium\\drivers\\chromedriver_win32\\chromedriver.exe");
driver = new ChromeDriver(options);
}
else if(browserType.equals("FireFox"))
{
driver = new FirefoxDriver();
}
}

@Test
public void openURL() throws IOException
{
Properties prop = new Properties();
FileInputStream fis = new FileInputStream("C:\\Users\\xxxxx\\URL.properties");
prop.load(fis);
driver.get(prop.getProperty("URL"));
WebDriverWait myDynamicElement = new WebDriverWait(driver,30); myDynamicElement.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//input[@name='userName']")));
}

@Test(dependsOnMethods={"openURL"})
public void loginToTours () throws InterruptedException, IOException
{
Properties prop = new Properties();
FileInputStream fis = new FileInputStream("C:\\Users\\xxxxx\\URL.properties");
prop.load(fis); driver.findElement(By.name("userName")).sendKeys(prop.getProperty("login")); driver.findElement(By.name("password")).sendKeys(prop.getProperty("password"));
driver.findElement(By.name("login")).click();
}
}

最佳答案

您可以将它们移动到 BeforeClass ,它只会在类的所有测试之前执行一次。我个人不喜欢启动浏览器、直接处理页面元素的测试的想法。我建议您实现页面对象模型 - 检查 here

public class CallUrl {
WebDriver driver;
Properties prop;

@BeforeClass
public void loadProps(){
prop = new Properties();
FileInputStream fis = new FileInputStream("C:\\Users\\xxxx\\URL.properties");
prop.load(fis);
}


@BeforeTest
public void openBrowser() throws IOException {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\xxxx\\chromedriver.exe");
String browserType = prop.getProperty("Browser");
if (browserType.equals("Chrome")) {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\Ashish\\Documents\\Selenium\\drivers\\chromedriver_win32\\chromedriver.exe");
driver = new ChromeDriver(options);
} else if (browserType.equals("FireFox")) {
driver = new FirefoxDriver();
}
}

@Test
public void openURL() throws IOException {
driver.get(prop.getProperty("URL"));
WebDriverWait myDynamicElement = new WebDriverWait(driver, 30);
myDynamicElement.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//input[@name='userName']")));
}

@Test(dependsOnMethods = {
"openURL"
})
public void loginToTours() throws InterruptedException, IOException {
driver.findElement(By.name("userName")).sendKeys(prop.getProperty("login"));
driver.findElement(By.name("password")).sendKeys(prop.getProperty("password"));
driver.findElement(By.name("login")).click();
}
}

关于java - 我不想在Java中重复代码selenium,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39860908/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com