在尝试使用 Selenium 的 POM 时遇到空指针异常。

huangapple go评论77阅读模式
英文:

Getting a NullPointerException when trying a pom in Selenium

问题

试图从零开始创建一个Selenium页面对象模型POM框架
类 1包括登录类 2包括 ur - 执行所有内容
我没有扩展任何文件
在 css 方法处出现异常不确定如何修复

Class 1
public class LOGIN {

    WebDriver driver;

    public LOGIN(WebDriver driver){
        this.driver = driver;
    }

    public void facebook(){
        System.setProperty("webdriver.chrome.driver", "C:\\Users\\Balu\\Desktop\\selenium\\chromedriver_win32\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.get("https://www.facebook.com/");
    }
}

Class 2 : 
public class Home  {

    WebDriver driver;

    public Home(WebDriver driver){
        this.driver = driver;
    }

    public void css(){	
        driver.manage().window().maximize();
    }
}

Class 3 :
public class Test1 {

    private static final WebDriver driver = null;

    @Test
    public void e2eflow(){
        LOGIN lg = new LOGIN(driver);
        Home hm = new Home(driver);
        lg.facebook();
        hm.css();
    }
}
英文:

Trying to create and Selenium POM framework from scratch .
Class 1 :consist of login, class 2 consist of ur ,class- executing everything .
i am not extended any file
Getting exception at css method not sure how to rectify

    Class 1 :
    public class LOGIN {
    
    WebDriver driver;
    	public LOGIN(WebDriver driver){
    		
    		this.driver = driver;
    	}
    	
    	public void facebook(){
    		System.setProperty("webdriver.chrome.driver", "C:\\Users\\Balu\\Desktop\\selenium\\chromedriver_win32\\chromedriver.exe");
    		WebDriver driver = new ChromeDriver();	
    		driver.get("https://www.facebook.com/");
    	}
    
    }    
    
    Class 2 : 
    public class Home  {
    	
    	WebDriver driver;
    
    	public Home(WebDriver driver){
    		this.driver = driver;
    			}
    	
    public void css(){	
 
     driver.manage().window().maximize();
    }
    	
    
    }
       
    
    Class 3 :
    public class Test1 {
    	
    	private static final WebDriver driver = null;
    
    	@Test
    	public void e2eflow(){
    		LOGIN lg = new LOGIN(driver);
    		Home hm = new Home(driver);
    		lg.facebook();
    		hm.css();
    		
    	}
    
    }

答案1

得分: 1

class LOGIN {

	public static WebDriver driver;
        public LOGIN(){
        	System.setProperty("webdriver.chrome.driver", "D:\\chromedriver.exe");
        	driver=new ChromeDriver();
        }
        
        public void facebook(){           
            driver.get("https://www.facebook.com/");
        }
    
    }    

class Home extends LOGIN {
    
public void css(){  
    driver.manage().window().maximize();
}
 }

class Test1 {
    
    @Test
    public void e2eflow(){
       
       Home hm = new Home();
       hm.facebook();
        hm.css();
        
    }

}
英文:
class LOGIN {

public static WebDriver driver;
    public LOGIN(){
    	System.setProperty("webdriver.chrome.driver", "D:\\chromedriver.exe");
    	driver=new ChromeDriver();
    }
    
    public void facebook(){           
        driver.get("https://www.facebook.com/");
    }

}    
class Home extends LOGIN {
    
public void css(){  

 driver.manage().window().maximize();
}
 }
class Test1 {
    
    @Test
    public void e2eflow(){
       
       Home hm = new Home();
       hm.facebook();
        hm.css();
        
    }

}

答案2

得分: 0

driverTest1 中为 null,这就是你得到异常的原因。

英文:

driver is null in Test1, that's why you're getting the exception

答案3

得分: 0

driver = new ChromeDriver() 替代 null。

英文:

driver = new ChromeDriver() instead of null.

答案4

得分: 0

在您的测试类中,您将驱动程序声明为null。
由于您想要使用POM模式,我建议您参考这个教程:POM教程

您可以拥有一个使用@BeforeTest标记的方法,在该方法中设置驱动程序,或者您可以在一个类中进行设置,并在所有测试类中进行扩展。

英文:

In your test classes you are declaring the driver as null.
As you want to use POM pattern, I suggest go trough this tutorial: POM tutorial

You can have a method marked with @BeforeTest where you are setting up your driver, or you can do it in a class and extend it in all test classes.

huangapple
  • 本文由 发表于 2020年8月24日 21:45:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/63562318.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定