首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在Selenium中获取配置方法失败

在Selenium中获取配置方法失败
EN

Stack Overflow用户
提问于 2016-02-15 12:38:20
回答 3查看 1K关注 0票数 2

我有两节课。(超级班和子班)

有10个@Test方法的子类。具有@AfterMethod和@ BeforeMethod的超类。

我使用TestNG在Selenium中运行子类。

如果第一个或任何一个测试方法失败,则跳过所有测试。

你能把相关的答案贴出来吗?

EN

回答 3

Stack Overflow用户

发布于 2016-02-15 12:51:31

您可以使用try...catch块添加每个方法。将你的测试代码放在try Block中,如果某些东西失败了,它将被Catch Block捕获。

代码语言:javascript
运行
复制
     @Test
        public void method1{
          try{
//your code including assertion
          }catch(Exception e){
        //exception message
        }
  }

实现断言对我来说很好,

代码语言:javascript
运行
复制
   boolean Flag = False;
//your code evaluate value of flag, if it is true test case will be passed otherwise failed and execution will be continue with other Test method

        try {
                        Assert.assertTrue("Not Working", Flag);
                    } catch (Exception ex) {
                        LOGGER.error("Exception in Method", ex);
                    }
票数 0
EN

Stack Overflow用户

发布于 2016-02-15 14:41:12

不确定为什么下面的you..but会失败,对我很有效:

代码语言:javascript
运行
复制
import org.testng.Assert;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.AfterMethod;
class Base {
    @BeforeMethod
    public void beforeMethod() {
        System.out.println("Before method");
    }

    @AfterMethod
    public void afterMethod() {
        System.out.println("After method");
    }

}
public class SO35401581 extends Base{
    @Test
    public void test1() {
        System.out.println("Run test1!!");
        Assert.fail("Fail");
    }

    @Test
    public void test2() {
        System.out.println("Run test2!!");
    }

    @Test
    public void test3() {
        System.out.println("Run test3!!");
    }
}

输出

代码语言:javascript
运行
复制
[TestNG] Running:

      <Path>\AppData\Local\Temp\testng-eclipse-583895345\testng-customsuite.xml

    Before method
    Run test1!!
    After method
    Before method
    Run test2!!
    After method
    Before method
    Run test3!!
    After method
    PASSED: test2
    PASSED: test3
    FAILED: test1
    java.lang.AssertionError: Fail
        at org.testng.Assert.fail(Assert.java:94)
        at practise.SO35401581.test1(SO35401581.java:12)
===============================================
    Default test
    Tests run: 3, Failures: 1, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 3, Failures: 1, Skips: 0
===============================================

我使用的是TestNG: 6.9.10 version..and,试图使用eclipse-testng插件运行上面的测试。

票数 0
EN

Stack Overflow用户

发布于 2016-02-15 15:14:12

子类

公共类US_FranchisePartner_English扩展了SuperTestNG {

代码语言:javascript
运行
复制
@Test(retryAnalyzer = Retry.class)
public void testFP_US_English()
{   


    System.out.println("***********************FranchisePartner_US_English_TransformationManager Pack***********************");

    Select country = new Select(driver.findElement(By.xpath("//select[@ng-model='selectedCountry']")));
    country.selectByIndex(7);


    Select language = new Select(driver.findElement(By.xpath("//select[@ng-model='settings.locale']")));
    language.selectByIndex(0);

    new HomePage(driver).Selectbusinessfp();

    new BasePage(driver).Continue();

    new InitialOrderPage(driver).TransformationManager();

}



@Test(retryAnalyzer = Retry.class)
public void testFP_US_Spanish()
{   

    System.out.println("***********************FranchisePartner_US_Spanish_TransformationJr.Manager Pack***********************");

    Select country = new Select(driver.findElement(By.xpath("//select[@ng-model='selectedCountry']")));
    country.selectByIndex(7);

    Select language = new Select(driver.findElement(By.xpath("//select[@ng-model='settings.locale']")));
    language.selectByIndex(1);

    // click continuar button in INICIO page
    new BasePage(driver).Continuar();

    // click on Gerente Jr. de Transformacion pack in ORDEN INICIAL
    new InitialOrderPage(driver).GerenteJrdeTransformacion();
}

@Test(retryAnalyzer = Retry.class)
public void testPC_US_English() 
{
    System.out.println("***********************PreferredCustomer_US_English_Transformation Pack***********************");

    Select country = new Select(driver.findElement(By.xpath("//select[@ng-model='selectedCountry']")));
    country.selectByIndex(7);

    Select language = new Select(driver.findElement(By.xpath("//select[@ng-model='settings.locale']")));
    language.selectByIndex(0);

    // click on preffered customer radio button
    new HomePage(driver).Selectbusinesspc();

    // click on continue button in homepage
    new BasePage(driver).Continue();

    // click on transformation pack radio button in initial order page
    new InitialOrderPage(driver).Transformation();
}

}

父类

代码语言:javascript
运行
复制
@BeforeMethod(alwaysRun=true)
public void beforeMethod() {
    System.setProperty("webdriver.chrome.driver","C:/Users/sumancb/Downloads/chromedriver_win32_19/chromedriver.exe");   
    driver = new ChromeDriver(); 

    // maximize the chrome browser
    driver.manage().window().maximize();
    // Time to wait for visible elements
    driver.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS);
    // enter the test url
    driver.get("http://enroll.ng.unicityqa.com/master/build/#/enroll/start");
}

@AfterMethod(alwaysRun=true)
public void afterMethod() {
    JavascriptExecutor IOpage2 = (JavascriptExecutor)driver;
    IOpage2.executeScript("scroll(0, 250)");

    JavascriptExecutor IOpage4 = (JavascriptExecutor)driver;
    IOpage4.executeScript("scroll(0, 250)");

    driver.findElement(By.xpath("(//button[@ng-click='searchProducts = search_all'])[2]")).click();

    JavascriptExecutor IOpage1 = (JavascriptExecutor)driver;
    IOpage1.executeScript("scroll(0, 250)");

    driver.findElement(By.xpath("(//span[text()='ADD'])[1]")).click();  

    JavascriptExecutor IOpage3 = (JavascriptExecutor)driver;
    IOpage3.executeScript("scroll(0, 250)");

    driver.findElement(By.xpath("(//span[text()='Suggesfdsgsted products'])[1]")).click();

    JavascriptExecutor IOpage43 = (JavascriptExecutor)driver;
    IOpage43.executeScript("scroll(0, 250)");  

    driver.findElement(By.xpath("(//span[text()='ADD'])[1]")).click();
    driver.close();
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35401581

复制
相关文章

相似问题

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