如何使用 Selenium Java 处理可重复使用的组件?


我们可以借助继承概念来处理 Selenium Java 中的可重复使用的组件。它是一种父子关系,其中子类继承父类的属性和方法。

示例

父类。

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Baseclass {
   public void login() throws IOException {
      Properties prop = new Properties();
      //Reading values from property file
      FileInputStream ips = new FileInputStream(
      "C:\Users\ghs6kor\eclipse- workspace\Inheritance\config.properties");
      prop.load(ips);
      System.setProperty("webdriver.gecko.driver",             "C:\Users\ghs6kor\Desktop\Java\geckodriver.exe");
      WebDriver driver = new FirefoxDriver();
      driver.get(prop.getProperty("url"));
   }
}

示例

子类。

import java.io.IOException;
public class Child extends Baseclass {
   public static void main(String[] args) throws IOException {
      // TODO Auto-generated method stub
      Child c = new Child();
      c.login();
      c.testinheritance();
   }
   public void testinheritance() {
      // parent class method used in child class
      login();
      System.out.println("Test Inheritance");
   }
}

更新于: 11-Jun-2020

928 次浏览

开启你的 职业生涯

完成课程即可获得认证证书

开始学习
广告
© . All rights reserved.