简体   繁体   中英

Selenium_testng_Java_passing data to both @beforetest and @test

I have my TesTng class with 3 test (A,B, C) and this class extends base class which has @beforemethod and @ aftermethod

Now I want to pass browser to @ before method and email to method A

Below is my sample data. 在此处输入图片说明

Email has to be unique every time.

one of the ways is to use @Parameters annotation.

Code for @BeforeMethod is-

@BeforeMethod
@Parameters("browser")
public void testMethod1(String browser) {
     //do your task here
}

Code for method A-

@Test
@Parameters("email")
public void A(String email) {
     //implement your test logic here
}

Sample TestNG Sample-

<suite name="Suite1" verbose="1" >

<test name="Test1">
  <parameter name="browser" value="firefox"/> 
  <parameter name="email" value="an-email-id"/> 
  <classes>
    <class name="packagename.ClassName"/>
  </classes>
</test>

</suite>

I found a solution for my problem please le me know if more efficient way is there.. I have userd @ parameter (thanx optimist_creeper) and In testng.xml I have created different test

<?xml version="1.0" encoding="UTF-8"?>

            <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
            <suite name="Suite">
            <test name="Chrome_test">
            <parameter name="Browser" value="chrome"></parameter>
            <parameter name="email" value="a"></parameter>
            <classes>
                <class name="selenium.mail.gmail.BabyTest">
                    <methods>
                        <include name="A" />
                        <include name="B" />
                        <exclude name="C" />
                    </methods>
        </class>
    </classes>
</test> <!-- Test -->

<test name="FF_test">
    <parameter name="Browser" value="Firefox"></parameter>
    <parameter name="email" value="b"></parameter>
    <classes>
        <class name="selenium.mail.gmail.BabyTest">
            <methods>
                <exclude name="C" />
            </methods>
        </class>
    </classes>
</test>


<test name="IE_test">
    <parameter name="Browser" value="IE"></parameter>
    <parameter name="email" value="c"></parameter>
    <classes>
        <class name="selenium.mail.gmail.BabyTest">
            <methods>
                <include name="A" />
            </methods>
        </class>
    </classes>
</test>

sorry for wrong indentation, code with correct indentation wasn't displayed properly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM