首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >工业级Selenium Webdrive模板

工业级Selenium Webdrive模板
EN

Stack Overflow用户
提问于 2018-08-20 19:09:27
回答 1查看 44关注 0票数 2

我最近开始学习selenium WebDriver,我从不同的来源学到了很多东西,但我不太清楚一个干净/专业的脚本应该是什么样子,以及它的内容应该如何编写。

这是我创建的一个作为测试的登录示例,我可以更改什么?

代码语言:javascript
复制
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();
    }
}
EN

回答 1

Stack Overflow用户

发布于 2018-08-20 21:44:02

您应该花一些时间学习页面对象模型。如果您要构建多个测试,这将对组织有很大的促进作用,使您的代码保持干净,并减轻维护负担。

避免Thread.sleep()和隐式等待。取而代之的是WebDriverWait

不要编写自己的日志记录/报告。请改用JUnit或TestNG。它们建立得很好,不仅可以记录日志,还可以处理测试、执行、报告等的组织工作,这将为您节省大量时间。

注意:对上的问题要小心,这样听起来就像是在要求代码审查。还有一个完全不同的网站,http://codereview.stackexchange.com

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51929469

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档