如何在运行selenium项目时使用java中另一个类的一个类的函数

时间:2017-05-03 08:53:00

标签: java selenium-webdriver

这里我构建了2个类,我想在类'Testjava'中使用函数“User_mgmt_A”。  我的第一堂课: -         公共类 testjava 扩展User_mgmt {

        void valid_login() 
        {

        System.setProperty("webdriver.chrome.driver", "C:/Users/neha.sharma/Downloads/chromedriver.exe");
        WebDriver driver = new ChromeDriver();
            driver.get("http://192.168.127.54:8080/TNSRDH/login");
            BufferedReader br1= new BufferedReader(new InputStreamReader(System.in));
            String user="test4pds";
            System.out.print("Enter the username for the Tamil Nadu project");
            try {
                user=br1.readLine();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }

            driver.findElement(By.id("username")).sendKeys(user);
            BufferedReader br2= new BufferedReader(new InputStreamReader(System.in));
            String pwd = "Pass@1234";
            System.out.println("Enter the password for Tamil Nadu project");
            try {
                pwd=br2.readLine();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            driver.findElement(By.id("txtPassword")).sendKeys(pwd);
           driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
            Select department=new Select(driver.findElement(By.name("department")));
            department.selectByIndex(3);
            driver.findElement(By.id("btn")).click();

        public static void main(String args[]) throws IOException 
        {
    testjava login= new testjava();
    User_mgmt mgmt=new User_mgmt();
    login.valid_login();
    mgmt.User_mgmt_A();
    }

    ---------------------------------------------------------
My 2nd class:-
    public class User_mgmt {
        void User_mgmt_A() throws IOException
        {
            System.setProperty("webdriver.chrome.driver", "C:/Users/neha.sharma/Downloads/chromedriver.exe");
            WebDriver driver = new ChromeDriver();
            /*driver.get("http://192.168.127.54:8080/TNSRDH/login");
            driver.findElement(By.id("username")).sendKeys("Test4pds");
            driver.findElement(By.id("txtPassword")).sendKeys("Pass@1234");
            Select department=new Select(driver.findElement(By.name("department")));
            department.selectByIndex(3);
            driver.findElement(By.id("btn")).click(); */
             WebDriverWait wait = new WebDriverWait(driver, 4000);
                wait.until(ExpectedConditions.visibilityOfElementLocated((By.id("id"))));
            driver.findElement(By.xpath("//a[contains(.,'User Management')]")).click(); //selecting user management icon
            driver.findElement(By.xpath("//a[contains(@href,'showUserSubModule?submodule=userCreation')]")).click(); // selecting 'User creation' link
            driver.findElement(By.xpath("//img[(@src='images/adduser.png')]")).click(); // selecting 'Add user' link

            driver.findElement(By.xpath(".//*[@id='userform']/div[4]/div[2]/div/button")).click(); // selecting department in Add user form
            driver.findElement(By.xpath(".//*[@id='userform']/div[4]/div[2]/div/div/ul/li[2]/label/input")).click(); // selecting 'CMUPT' Department
            driver.findElement(By.id("firstName")).sendKeys("ArunTester");
            driver.findElement(By.id("middleName")).sendKeys("Srinivas");
            driver.findElement(By.id("lastName")).sendKeys("B");
            driver.findElement(By.id("loginName")).sendKeys("ArunTester12");
           /* List RadioBtn= (List) driver.findElement(By.xpath("//div[@class='col-lg-4 col-md-4 col-xs-4 col-sm-4']"));
            BufferedReader Radio= new BufferedReader(new InputStreamReader(System.in));
            String radioM="Male";
            radioM=Radio.readLine();
            if (radioM.equals(Radio))
            {
                driver.findElement(By.id("sex1")).click();
            }

            driver.findElement(By.id("sex")).click();

             */      

        }

        private WebElement RadioBtn(int i) {
            // TODO Auto-generated method stub
            return null;
        }

    public static void main(String args[]) throws IOException 
    {
        System.exit(0);

    }
    }

请告诉我如何在不同的课程中调用函数。我尝试使用'Extend'关键字,但它没有用。

2 个答案:

答案 0 :(得分:0)

您可能希望首先学习面向对象编程(OOP)的基础知识。 据我了解你,你想扩展你的课程" testjava"与班级" User_mgmt"为了在" testjava"中使用方法User_mgmt_A()。

首先要做的事情:

对于上帝的爱,请阅读Java Code Conventions

您正在使用的类名和方法名称是不可理解的。

接下来,正如我之前提到的,赶上OOP。

如果你的" User_mgmt"真的应该是" testjava"的基类。然后参考以下示例代码使其在您自己的代码库中工作:

文件:Pet.java

package de.myproject.samples;
class Pet{
 private String name;
 protected String getName(String name){return this.name;}
 public Pet(String name){
   this.name=name;
 }
}

档案:Dog.java

package de.myproject.samples;
class Dog extends Pet{
 public Dog(String name){
  super(name);
  System.out.println("The dogs name is "+this.getName());
 }
}

答案 1 :(得分:0)

在java中,您将其称为methods而不是函数,您可以通过在另一个类中创建一个类的对象并使用该对象调用它来访问它。