Found 346 Articles for Java Programming

How will you run a prerequisite method and post condition method forevery test in TestNG?

Debomita Bhattacharjee
Updated on 11-Jun-2020 12:47:26

215 Views

We can run a prerequisite method and post condition method for every test in TestNG with the help of @BeforeMethod and @AfterMethod annotations.Example@BeforeMethod public void prerequisite(){    System.out.println("Run before every tests"); } @AfterMethod public void postcondition(){    System.out.println("Run after every tests "); } @Test public void loanPay(){    System.out.println("Loan pay is successful"); }In the Java class file, the prerequisite() method with @BeforeMethod will be executed which is known as the precondition for every test method. Then loanPay() will be executed and finally the postcondition() method with @AfterMethod will run.

How to use regular expressions with TestNG?

Debomita Bhattacharjee
Updated on 11-Jun-2020 12:45:23

890 Views

We use regular expressions in TestNG to work with a group of test methods that are named with a certain pattern.ExampleTestng xml file.                                                               All the test methods with starting name Payment will be excluded from the regression suite.Example@Test public void PaymentHistory(){    System.out.println("Payment history validation is successful”); } @Test public void Login(){    System.out.println("Login is successful”); } @Test public void PaymentDefault(){    System.out.println("Payment default verification is successful”); }Login() will be executed, but all the methods starting with name Payment will be excluded from execution. This is achieved using regular expression(Payment.*).

How to set priority to the test cases in TestNG?

Debomita Bhattacharjee
Updated on 11-Jun-2020 12:43:32

514 Views

We can set priority for test cases in order of their execution, by giving priority to each test method. A test method having lower priority runs first then the test methods with higher priority are executed.Example@Test (priority = 1) public void verifyTravel(){    System.out.println("Travel history successful "); } @Test (priority = 2) public verifyIncome(){    System.out.println ("Income history successful"); }In the Java class file, verifyTravel() will run first followed by verifyIncome().

How can a particular group of test cases get executed in TestNG?

Debomita Bhattacharjee
Updated on 11-Jun-2020 12:41:02

930 Views

We can run a particular set of test cases by including a group of test cases in the execution.ExampleTestng xml files with groups.                                                                           To run a group of test cases from the set of test cases, we have to define in the testng xml file. Here the testNG xml contains group Smoke to be included in execution.Example@Test(groups={"Smoke"}) public void Payment(){    System.out.println(“Payment is successful”); }In the Java class file only the test method with group as Smoke will be run out of the entire regression suite.

How to overlook a particular test method from execution in TestNG?

Debomita Bhattacharjee
Updated on 11-Jun-2020 12:31:45

266 Views

To overlook a particular test method from execution in TestNG enabled helper attribute is used. This attribute has to be set to false to overlook a test method from execution.ExampleJava class file.@Test(enabled=false) public void verifyRepay(){    System.out.println("Repayment successful"); } @Test public void Login(){    System.out.println("Login is successful "); } @Test public verifyHistory(){    System.out.println ("History verification is successful"); }Here the verifyRepay() method shall be overlooked during execution.

How does the execution of a particular test method depend on other testmethods in TestNG?

Debomita Bhattacharjee
Updated on 11-Jun-2020 12:29:24

222 Views

The execution of a particular test method can be made dependent on another test method with the help of dependsOnMethods helper attribute.Example@Test(dependsOnMethods={"Payment"}) public void verifyLoan(){    System.out.println("Loan payment successful"); } @Test public void Payment(){    System.out.println("Payment successful "); } @Test public verifyTransaction(){    System.out.println ("Transaction verification"); }Here in the Java class file, verifyLoan() method will only be executed after the Payment() method is run successfully. But verifyTransaction() method runs independently without having a precondition test method to be executed.

What is the purpose of the testng.xml file?

Debomita Bhattacharjee
Updated on 11-Jun-2020 12:25:18

1K+ Views

The testng.xml file has the numerous uses as listed below −Test cases are executed in groups.Test methods can be included or excluded in the execution.The execution of multiple test cases from multiple java class files can be triggered.Comprises names of the folder, class, method.Capable of triggering parallel execution.Test methods belonging to groups can be included or excluded in the execution.ExampleTestNG.xml file                                                         Here as per the xml file, ... Read More

What does implicit wait perform?

Debomita Bhattacharjee
Updated on 10-Jun-2020 13:52:48

510 Views

Implicit is the default waiting time for each test step in our execution. Thus if we keep an implicit wait of ten seconds, each test step will wait for that amount of time for an action to take place and then move to the next step.Implicit wait is a dynamic wait which means if the wait time is ten seconds and the web element on which the next action is to be taken is available at the fifth second, then control will immediately move to the next test step rather than waiting for the full ten seconds.However if the element ... Read More

What are the differences between get() and navigate() method?

Debomita Bhattacharjee
Updated on 10-Jun-2020 13:32:34

7K+ Views

The differences between get() and navigate() methods are listed below.sl.no.get()navigate()1It is responsible for loading the page and waits until the page has finished loading.It is only responsible for redirecting the page and then returning immediately.2It cannot track the history of the browser.It tracks the browser history and can perform back and forth in the browser.ExampleWith get().import org.openqa.selenium.By; import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; import java.util.List; public class LaunchBrw {    public static void main(String[] args) {       System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");       WebDriver driver = new ChromeDriver();       String url = "https://www.tutorialspoint.com/index.htm"; ... Read More

What are the differences between findElement() and findElements() methods?

Debomita Bhattacharjee
Updated on 10-Jun-2020 13:19:00

776 Views

findElement() and findElements() method tries to search an element in DOM.The differences between them are listed below −sl.no.findElement()findElements()1It returns the first web element which matches with the locator.It returns all the web elements which match with the locator.2Syntax − WebElement button = webdriver.findElement(By.name(""));Syntax − List buttons = webdriver.findElements(By.name(""));3NoSuchElementException is thrown if there are no matching web elementsEmpty list is returned if there are no matching elements.ExampleUsing findElements ().import org.openqa.selenium.By; import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; public class RowFindElements {    public static void main(String[] args) {       System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");       WebDriver driver ... Read More

Advertisements