How to optimize testng and seleniums tests

后端 未结 3 1467
陌清茗
陌清茗 2021-02-02 04:12

For my internship, I have to use TestNG and selenium for testing a web-application. But I have a problem, sometimes selenium or the Browser is not working for some random reason

3条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-02 04:39

    The other answers are the "right" way. For a "quick'n'dirty" way just move the actual test code to a private method. In your annotated test method, make a for-loop with a try..catch inside where you look for Throwable (superclass of all errors and exceptions). On error just continue loop or throw the last error, on success break loop.

    Sample code:

    @Test
    public void testA() throws Throwable {
        Throwable err = null;
        for (int i=0; i<4; i++) {
            try {
                testImplementationA();
                return;
            } catch (Throwable e) {
                err = e;
                continue;
            }
        }
        throw err;
    }
    
    private void testImplementationA()
    {
        // put test code here
    }
    

    Usually, though, it's better to write tests that do not fail randomly. Also using this approach you don't have information about how many tries failed, which is an important information after all. I personally rather re-run the whole test class/suite on Jenkins on failure, and investigate why it failed.

提交回复
热议问题