从其他包中调用Java类

时间:2019-09-05 06:19:01

标签: selenium

我想使用整个Java类,以高效地编写测试脚本。无需再次在课堂上重写它。来自Create_PurchaseReceive.java的另一个软件包中的Login.java。我想使用login.java,所以我不会在Create_purchaseReceive.java上再次重写。

尝试在Google上搜索并尝试调用public voidextendbase

login.java

package Login;
public class Login {

WebDriver driver;


//Open Brower           
@BeforeTest 
public void LoginWebSystem() {              
    driver = new ChromeDriver();    
    driver.get("http://localhost:82");  
    WebElement email = driver.findElement(By.id("login_username"));
    email.sendKeys("superadmin");   
    System.out.println("Username Set"); 
    WebElement password = driver.findElement(By.id("login_password"));
    password.sendKeys("nelsoft121586"); 
    System.out.println("Password Set"); 
    WebElement login = driver.findElement(By.id("login_submit"));
    login.click();
    System.out.println("Login Button Clicked"); 
}       
//If login is successful or failed
@Test (priority=1)
public void LoginAccount() {
String newUrl = driver.getCurrentUrl();

if(newUrl.equalsIgnoreCase("http://localhost:82/controlpanel.php")){
    System.out.println("Login Success");
}
else {
    System.out.println("Login Failed");

create_purchasereceive.java

package PurchaseModule;
public class Create_PurchaseReceive {

WebDriver driver;


//Open Brower           
@BeforeTest 
public void LoginWebSystem() {              
    driver = new ChromeDriver();    
    driver.get("http://localhost:82");  
    WebElement email = driver.findElement(By.id("login_username"));
    email.sendKeys("superadmin");   
    System.out.println("Username Set"); 
    WebElement password = driver.findElement(By.id("login_password"));
    password.sendKeys("nelsoft121586"); 
    System.out.println("Password Set"); 
    WebElement login = driver.findElement(By.id("login_submit"));
    login.click();
    System.out.println("Login Button Clicked"); 
}       
//If login is successful or failed
@Test (priority=1)
public void LoginAccount() {
String newUrl = driver.getCurrentUrl();

if(newUrl.equalsIgnoreCase("http://localhost:82/controlpanel.php")){
    System.out.println("Login Success");
}
else {
    System.out.println("Login Failed");
}
}
//Proceed to Purchase Order page
@Test (priority=2)
public void PurchaseReceivePage() {
    WebDriverWait wait = new WebDriverWait(driver, 10);
    WebElement element = wait.until(
            ExpectedConditions.visibilityOfElementLocated(By.partialLinkText("Purchase Receive")));

    driver.findElement(By.partialLinkText("Purchase Receive")).click();
    System.out.println("Successful in proceeding to Purchase Receive");
}
@Test (priority=3)
public void NewPurchaseRecieve() {
    driver.findElement(By.className("bttn-imp-create")).click();
    System.out.println("Successful in proceeding to Purchase_receive.php");

    String newUrl1 = driver.getCurrentUrl();

    if(newUrl1.equalsIgnoreCase("http://localhost:82/purchase.php")){
        System.out.println("Successful in proceeding to Purchase Receive page ");
    }
    else {
        System.out.println("Failed in proceeding to Purchase Receive page");
    }
}
 @Test (priority=4)
    public void SelectInvoice() {

        driver.findElement(By.id("select-request-invoice")).click();
        System.out.println("Successful in clicking select invoice");

        WebElement selectinvoice = driver.findElement(By.id("select-request-invoice"));
        selectinvoice.click();



 }

         @Test (priority=6)
        public void SearchInvoice() {

            WebDriverWait waitdateFROM = new WebDriverWait(driver, 25);
            waitdateFROM.until(ExpectedConditions.elementToBeClickable(By.id("date-purchase-from")));

            WebElement dateFROM = driver.findElement(By.id("date-purchase-from"));
            dateFROM.sendKeys(Keys.chord(Keys.CONTROL, "a"), "2019-09-04",Keys.ENTER); 
            System.out.println("Successful in changing the date from");

            WebElement dateTO = driver.findElement(By.id("date-purchase-to"));
            dateTO.sendKeys(Keys.chord(Keys.CONTROL, "a"), "2019-09-05",Keys.ENTER); 
            System.out.println("Successful in changing the date to");

            WebDriverWait waitdateTO = new WebDriverWait(driver, 25);
            waitdateTO.until(ExpectedConditions.elementToBeClickable(By.className("bttn-search")));

            WebElement searchbutton = driver.findElement(By.className("bttn-search"));
            searchbutton.click();
            System.out.println("Successful in clicking searchinvoice");

     }
}

我希望将login.java用于create_purchasereceive.java

1 个答案:

答案 0 :(得分:0)

您可以如下所示将登录类扩展到Create_PurchaseReceive中。它可以解决您的问题。

package PurchaseModule;
public class Create_PurchaseReceive extends Login {
}