简体   繁体   中英

How to run multiple test cases in a multiple paged application with TestNG

This is the testng.xml

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Test Suite" parallel="methods" >
    <test name="Tools QA" > 
        <classes>
            <class name="automation.Prescan">
                <methods>
                <include name= "Startup" />
                <include name ="LoginTest" />
                <include name="EntryTest" />
                </methods>
            </class>
        </classes>
    </test>
</suite>

followed by my firstTest.java file package automation;

import org.testng.annotations.*;
import org.testng.annotations.BeforeMethod;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
import org.testng.Assert;

public class Prescan {
    String baseURL = "https://abc/login";
    public WebDriver driver;

    @BeforeMethod
    public void Startup() {
        System.setProperty("webdriver.chrome.driver", "C:\\ChromeDriver\\chromedriver.exe");
        System.setProperty("webdriver.gecko.driver", "C:\\GeckoDriver\\geckodriver.exe");
        // initialize driver
        driver = new ChromeDriver();
        // driver = new FirefoxDriver();
        // driver.get(baseURL);
    }

    // @Test (description="Prescan login")
    @Test(priority = 1)
    public void PrescanLogin() throws Exception {
        driver.get(baseURL);
        driver.findElement(By.id("clientCode")).sendKeys("A");
        driver.findElement(By.id("username")).sendKeys("lates");
        driver.findElement(By.id("password")).sendKeys("Ma4");
        driver.findElement(By.xpath("//button[contains(.,'Login')]")).click();
        Assert.assertEquals("Welcome", driver.getTitle());
        Thread.sleep(4000);
    }

    // @Test (description="Pres")
    @Test(priority = 2)
    public void PreEntry() throws Exception {
        // driver.switchTo().frame("frame");
        driver.findElement(By.name("account_number")).sendKeys("A7664685W");
        driver.findElement(By.name("inv_date_month")).sendKeys("17");
        driver.findElement(By.name("inv_date_day")).sendKeys("07");
        driver.findElement(By.name("add")).click();

        Assert.assertEquals("Client Login", driver.getTitle());

        Thread.sleep(4000);

    }

    @AfterClass
    public void exit() {
        driver.quit();
    }
}

On running this test, I am getting this error

PASSED: PrescanLogin
FAILED: PrescanEntry
org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"name","selector":"account_number"}
  (Session info: chrome=56.0.2924.87)
  (Driver info: chromedriver=2.27.440174 )

When i execute above mentioned xml file, my execution stops.

Can anyone help me on this issue?

judging from your error; your test is failing here:

driver.findElement(By.name("account_number")).sendKeys("A7664685W");

This could be for a number of reasons. It will help if you could update your answet with some html code of the elements that you are trying to locate. However, from what you have sent already sent; it looks that these elements are sitting within a frame and you are not switching on it as you have commented the lines. Try to add this line below your after removing the // from the frame (do you know the frame name?):

 driver.switchTo().frame("frame");

After you finish locating elements within the frame you can go back with this:

driver.switchTo().defaultContent();

Also, this method of finding elements like this (without prior waiting from them is not advised, as your selenium script trips over..itself trying to find the element way before it has loaded on the page). So to avoid this use waitForElement:

WebDriverWait wait = new WebDriverWait(driver, timeout); 
Boolean elementPresent = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(XpathLocator)).isDisplayed();

The above will reutrn TRUE if the element is present and FALSE if it isn't.

Best of luck!

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