Reputation: 39
I've recently started learning about selenium WebDriver, and I've learned a lot of stuff from different sources but I don't have a good idea of how should a clean/professional grade script should look like and how should it's content be written.
This is an example of a login that I've created as a test, what could I change?
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();
}
}
Upvotes: 2
Views: 60
Reputation: 25628
You should spend some time learning about the Page Object Model. If you are going to build more than a few tests, it will be a significant boost to organization, keeping your code clean, and lessening the maintenance burden.
Avoid Thread.sleep()
and implicit waits. Instead prefer WebDriverWait
.
Don't write your own logging/reporting. Instead use JUnit or TestNG. They are well established and will save you a lot of time with not only logging but handling organization of your tests, executions, reporting, etc.
NOTE: Be careful about questions on SO that sound like asking for a code review. There's a whole other site for that, http://codereview.stackexchange.com.
Upvotes: 2