英文:
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
driver 在 Test1 中为 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论