我最近开始学习selenium WebDriver,我从不同的来源学到了很多东西,但我不太清楚一个干净/专业的脚本应该是什么样子,以及它的内容应该如何编写。
这是我创建的一个作为测试的登录示例,我可以更改什么?
package Facebook;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class Login {
WebDriver driver = new ChromeDriver();
public void login() throws InterruptedException
{
driver.get("http://www.facebook.com");
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
WebElement user = driver.findElement(By.id("email"));
WebElement password = driver.findElement(By.id("pass"));
user.sendKeys("user_test");
password.sendKeys("password_test");
Thread.sleep(3000);
user.clear();
password.clear();
WebElement submit = driver.findElement(By.id("u_0_u"));
if(submit.isDisplayed())
{
System.out.println("\u001B31;1m Succes");
}
else
{
System.out.println("\u001B31;2m Fail");
}
}
public static void main(String[] args) throws InterruptedException {
Login obj = new Login();
obj.login();
}
}发布于 2018-08-20 21:44:02
您应该花一些时间学习页面对象模型。如果您要构建多个测试,这将对组织有很大的促进作用,使您的代码保持干净,并减轻维护负担。
避免Thread.sleep()和隐式等待。取而代之的是WebDriverWait。
不要编写自己的日志记录/报告。请改用JUnit或TestNG。它们建立得很好,不仅可以记录日志,还可以处理测试、执行、报告等的组织工作,这将为您节省大量时间。
注意:对上的问题要小心,这样听起来就像是在要求代码审查。还有一个完全不同的网站,http://codereview.stackexchange.com。
https://stackoverflow.com/questions/51929469
复制相似问题