2016-11-09 61 views
0

我正在使用硒自动测试一个网站的项目。这是我要运行我的主类:在另一个类中调用类

package Login; 

import static org.junit.Assert.assertTrue; 

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; 
import org.openqa.selenium.firefox.FirefoxDriver; 
import org.openqa.selenium.ie.InternetExplorerDriver; 
import org.openqa.selenium.support.ui.ExpectedConditions; 
import org.openqa.selenium.support.ui.WebDriverWait; 
import org.testng.annotations.Parameters; 
import org.testng.annotations.Test; 

public class Test{ 

WebDriver driver; 
String baseUrl; 

private StringBuffer verificationErrors = new StringBuffer(); 

@Test 
@Parameters("browser") 
public void LoginDebiteurVerkeerdPage(String browserName) { 

    if(browserName.equalsIgnoreCase("firefox")) 
    { 
     System.setProperty("webdriver.gecko.driver","C:\\Users\\cursus\\Downloads\\geckodriver.exe"); 
     driver=new FirefoxDriver(); 
    } 
    else if(browserName.equalsIgnoreCase("chrome")) 
    { 
     System.setProperty("webdriver.chrome.driver", "C:\\Users\\cursus\\Downloads\\chromedriver.exe"); 
     driver=new ChromeDriver(); 
    } 
    else if(browserName.equalsIgnoreCase("IE")) 
    { 
     System.setProperty("webdriver.ie.driver", "C:\\Users\\cursus\\Downloads\\IEDriverServer_x64_2.53.1\\IEDriverServer.exe"); 
     driver=new InternetExplorerDriver(); 
    } 

    driver.manage().window().maximize(); 
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); 
    baseUrl = "https://www.l.nl/"; 
    driver.get(baseUrl + "/"); 

    // Testscases 
    Here i wanna invoke a few testcases that are in other classes. 

    // For example: LoginLogout (class LoginLogout) 
    // For example: LoginWrongusername (class LoginWrongusername) 
    // For example: LoginWrongpassword (class Loginwrongpassword) 

    driver.close(); 
} 

} 

我想有测试用例在其他类,所以它的结构将和可维护性。

如何在我的“测试”类中调用这些类(这是我的测试用例)?

感谢, 皮特

回答

0

您可以定义内的任何要在测试执行,并添加上的所有场景这些类的一些静态方法。例如。

public class LoginLogout(){ 

    public static executeScenarios(Driver driver){ 
     //your code here 
    } 
} 

在您的测试类:

LoginLogout.executeScenarios(driver); 
LoginWrongusername.executeScenarios(driver); 
... 

即使你可以把所有你的类与场景从那里驱动程序初始化一个共同的类扩展,而你只需要通过浏览器该方法。

希望它能帮到你