java 我可以将Soft Assert对象作为方法参数传递吗?

jq6vz3qz  于 2023-04-28  发布在  Java
关注(0)|答案(1)|浏览(118)

现有框架的设计方式如下。我们在VerifyAttributes中Assert一些检查。问题是,我想将Assert更改为软Assert,以便测试不会在单个Assert失败时停止,并且测试继续。如何使用单个软Assert对象进行所有检查?我可以在测试中的父类中创建一个软Assert,并在调用方法时将其作为属性传递吗?

public class TestClass extends BaseTest {

@Test(description="test to verify if the keyboard and mouse attributes are generated)
public void testKeyboardMouseAttributes(String os, String browser, String device){
//User performs some keyboard and mouse actions
InterfaceToVerifyAttributes verifyAttr = new InterfaceToVerifyAttributes("testKeyboardMouseAttributes",os,browser,device);
verifyAttr.keyboardAttributes();
}
}

public class InterfaceToVerifyAttributes {
    private String os, browser, device, testcase;
    public InterfaceToVerifyAttributes(String tc, String os, String browser, String device) {
        this.os = os;
        this.browser = browser;
        this.device = device;
        this.testcase=tc;
    }
public void keyboardAttributes(){
VerifyAttributes verifyAttr = new VerifyAttributes(testcase, os, browser, device);
        verifyAttr.verifyUnknownSession(false);
        verifyAttr.keyboardAttributes();
}}

public class VerifyAttributes{
private String os, browser, device, orgId;
//getters and setters

public void verifyUnknownSession(boolean expected) {
            Assert.assertNotNull(api.getSpecifiedParameter("unknown_session"),
                    " ** should be unknown session ** " + testcase + " " + os + " " + browser + " org_id " + orgId);
}
public void verifyKeyboardPageElements() {
        Assert.assertNotNull(//attribute is present);
Assert.assertTrue(//attribute value is equal to something);
//A few other assertions
      }
public void verifyMouseElements() {
        Assert.assertNotNull(//attribute is present);
Assert.assertTrue(//attribute value is equal to something);
//A few other assertions
      }
public void verifyPasswordType() { //This method is not used but is present
        Assert.assertNotNull(//attribute is present);
Assert.assertTrue(//attribute value is equal to something);
//A few other assertions
      }

}
ldfqzlk8

ldfqzlk81#

我能够做到这一点

public class TestClass extends BaseTest {

@Test(description="test to verify if the keyboard and mouse attributes are generated)
public void testKeyboardMouseAttributes(String os, String browser, String device){
SoftAssert sftp = new SoftAssert();
//User performs some keyboard and mouse actions
InterfaceToVerifyAttributes verifyAttr = new InterfaceToVerifyAttributes(os,browser,device,sftp);
sftp = verifyAttr.keyboardAttributes(sftp);
}
sftp.assertAll();
}

public class InterfaceToVerifyAttributes {
    private String os, browser, device;
SoftAssert sftp;
    public InterfaceToVerifyAttributes(String os, String browser, String device, SoftAssert sftp) {
        this.os = os;
        this.browser = browser;
        this.device = device;
        this.sftp = sftp;
    }
public SoftAssert keyboardAttributes(SoftAssert sftp){
VerifyAttributes verifyAttr = new VerifyAttributes( os, browser, device, sftp);
        sftp = verifyAttr.verifyUnknownSession(sftp);

}

return sftp;
}

public class VerifyAttributes{
private String os, browser, device;
SoftAssert sftp;

 public VerifyAttributes(String os, String browser, String device, SoftAssert sftp) {
        this.os = os;
        this.browser = browser;
        this.device = device;
        this.sftp = sftp;
}

public SoftAssert verifyUnknownSession(SoftAssert sftp) {
            sftp.assertNotNull(api.getSpecifiedParameter("unknown_session"),
                    " ** should be unknown session ** " + os + " " + browser);
}

return sftp;
}

相关问题