【问题标题】:Unable to call method which performing mouse actions to TestNG test case using selenium Java无法使用 selenium Java 调用对 TestNG 测试用例执行鼠标操作的方法
【发布时间】:2021-08-06 13:00:28
【问题描述】:
Main Block:
    public class test3 extends utility {
        public WebDriver driver;
        @BeforeTest
        public void invokeBrowser() throws IOException {
            driver = base();
            driver.get(resource.getProperty("url"));
    
        }
    
        @Test
        public class thirdPage {
            public void main() {
    
                actionTest test3 = new actionTest();
                test3.executeTest();
            }
        }
    
    }

执行鼠标操作的代码:

public class actionTest {   
    
    public void executeTest() {
    Actions execute = new Actions();    
    execute.moveToElement(driver.findElement(By.id("header-search-input"))).click().keyDown(Keys.SHIFT).sendKeys("Cricket").build().perform();
    driver.findElement(By.id("header-desktop-search-button")).click();
    }
    
    

}

在执行块 test3.executeTest() 时,上面的代码导致“NullPointerException”声明 driver = null;

【问题讨论】:

  • 什么返回base()方法?请添加它的代码。除此之外检查是否没有WebDriver driver 的另一个声明,可能在utility 类中。
  • Btw 类应该以首字母大写、Test3、Utility、ActionTest 等命名。
  • @pgurbr : base() 方法在实用程序类下将初始化 WebDriver WebDriver driver = new ChromeDriver(); 。澄清点是具有鼠标动作活动的 executeTest() 方法是否会被调用到@Test case thirdPage 类。 (或)有没有其他方法可以调用 executeTest() 到 @Test case 类。谢谢!
  • base() 方法必须返回 Webdriver 对象(作为 WebDriver 类型)或为声明的 WebDriver 对象设置值(作为 void)。
  • @pburgr :谢谢,我从您的建议中得到了问题! (抱歉在我之前的回复中提到了错误的名字)

标签: java selenium selenium-webdriver browser-automation


【解决方案1】:

重组您的代码库可能会帮助您解决此问题。创建一个基础测试类。在此处添加与 Web 驱动程序初始化和其他主要内容相关的代码。然后为您的测试创建另一个类。从基本测试类继承它。然后你就可以在你所有的测试类中使用初始化的驱动了。

【讨论】:

  • 感谢您的评论,我找到了修复程序并且它正在工作!
  • 太棒了! @SukumarSiva
猜你喜欢
  • 1970-01-01
  • 2021-07-13
  • 2016-10-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-14
  • 2018-03-05
相关资源
最近更新 更多