[英]In my Junit test case, all test after the first one fail
因此,我有了这个基本的JUnit程序,在@beforeclass中,我打开浏览器并将其最大化,然后打开浏览器导航到一个站点并检查该元素。 但是,始终只执行第一种情况。 所有后续测试用例均失败。 任何人都告诉我我在这里犯什么错误
码:
package JUnitTesting;
import static org.junit.Assert.*;
import java.util.concurrent.TimeUnit;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class FlockTeamURLDomain {
static WebDriver driver;
String TeamUrl = "http://farzanshaikh.flock.co/";
String TeamName = "<script>farzan</script>";
String TeamDomain = "farzanshaikh.com";
@BeforeClass
public static void setUpBeforeClass() throws Exception {
System.setProperty("webdriver.chrome.driver", "C:\\Automation\\chromedriver_win32\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
Thread.sleep(2000);
}
@Test
public void OpenTheTeamURL() throws InterruptedException {
driver.get(TeamUrl);
driver.manage().timeouts().implicitlyWait(3000, TimeUnit.SECONDS);
String Title = driver.getTitle();
System.out.println("The title of the Page is: "+Title);
if(Title.equals("Flock - Team")){
System.out.println("The Title is Correct");
}
else{
System.err.println("The Title is InCorrect");
}
Thread.sleep(2000);
}
@Test
public void CheckTheFooter() {
boolean FlockFooter = driver.findElement(By.cssSelector("//div[@id='team-page']/div[2]/div[5]")).isDisplayed();
System.out.println("Is the Footer Present? "+FlockFooter);
}
@Test
public void CheckAndClickLogo() {
boolean FlockLogo = driver.findElement(By.xpath("//div[@id='team-page']//img")).isDisplayed();
System.out.println("Is Flock Logo Displayed "+FlockLogo);
}
@Test
public void CheckTheHeader() {
boolean FlockHeaderLogo = driver.findElement(By.xpath("//div[@id='step-2-block']/span")).isDisplayed();
System.out.println("Is the Header Element Present? "+FlockHeaderLogo);
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
Thread.sleep(3000);
driver.quit();
}
}
我认为这是因为您仅在第一个测试中导航到所需的URL,而没有其他测试在导航至所需的URL
尝试将导航步骤添加到setUpBeforeClass方法中
试试下面:
package JUnitTesting;
import static org.junit.Assert.*;
import java.util.concurrent.TimeUnit;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class FlockTeamURLDomain {
static WebDriver driver;
String TeamUrl = "http://farzanshaikh.flock.co/";
String TeamName = "<script>farzan</script>";
String TeamDomain = "farzanshaikh.com";
@BeforeClass
public static void setUpBeforeClass() throws Exception {
System.setProperty("webdriver.chrome.driver", "C:\\Automation\\chromedriver_win32\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
Thread.sleep(2000);
driver.get(TeamUrl);
}
@Test
public void OpenTheTeamURL() throws InterruptedException {
driver.manage().timeouts().implicitlyWait(3000, TimeUnit.SECONDS);
String Title = driver.getTitle();
System.out.println("The title of the Page is: "+Title);
if(Title.equals("Flock - Team")){
System.out.println("The Title is Correct");
}
else{
System.err.println("The Title is InCorrect");
}
Thread.sleep(2000);
}
@Test
public void CheckTheFooter() {
boolean FlockFooter = driver.findElement(By.cssSelector("//div[@id='team-page']/div[2]/div[5]")).isDisplayed();
System.out.println("Is the Footer Present? "+FlockFooter);
}
@Test
public void CheckAndClickLogo() {
boolean FlockLogo = driver.findElement(By.xpath("//div[@id='team-page']//img")).isDisplayed();
System.out.println("Is Flock Logo Displayed "+FlockLogo);
}
@Test
public void CheckTheHeader() {
boolean FlockHeaderLogo = driver.findElement(By.xpath("//div[@id='step-2-block']/span")).isDisplayed();
System.out.println("Is the Header Element Present? "+FlockHeaderLogo);
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
Thread.sleep(3000);
driver.quit();
}
}
BeforeClass只运行一次。
尝试使用@Before代替。 该注释使该方法在每个测试用例之前运行。
如果我正确理解您希望测试执行先前测试权限遗留的内容,那么您需要确保执行顺序正确,尽管恕我直言,这不是应该创建测试的方式
我认为最好按顺序排列
def smoke_test_flock_team_domain():
""" test to check completeness of the page """
open the page
assert title
assert footer
assert header
assert logo
click logo and assert result
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.