java.lang.NullPointerException while invoking driver.quit() within #AfterClass annotated method through Selenium WebDriver and JUnit - selenium

I created a test using JUnit and in class #AfterClass I put the driver.quit () command to shut down the browser when the tests are finished but the eclipse displays the java.lang.NullPointerException message.
The test class populates several fields and then makes a query in the base, displays the result in the Eclipse console and should close the browser but displays the java.lang.NullPointerException message.
Below is the log and test script.
public class validarStatus {
private static WebDriver driver;
#Before
public void setUp() throws Exception {
System.setProperty("webdriver.chrome.driver", "E:\\Selenium\\chromedriver.exe");
}
#Test
public void validarStatusOs() throws InterruptedException {
WebDriver driver = new ChromeDriver();
driver.get("http://10.5.9.45/BKOMais_S86825EstrategiaBackOfficeClaroFixo");
driver.manage().window().maximize();
// Logar BkoMais
driver.findElement(By.id("matricula_I")).sendKeys("844502");
driver.findElement(By.id("senha_I")).sendKeys("Pw34Jdt#*");
driver.findElement(By.id("bt_entrar")).click();
// Logar na Estratégia
driver.findElement(By.id("mn_backoffice")).click();
driver.findElement(By.id("mn_bkoffice_prod_203")).click();// Produto
driver.findElement(By.id("mn_bkoffice_est_57")).click();// Estratégia
// Selecionado a atividade
Select atividade = new Select(driver.findElement(By.id("cboAtividade")));
atividade.selectByIndex(3);
// Registro >> Novo
Thread.sleep(500);
driver.findElement(By.id("mn_registro")).click();
driver.findElement(By.id("mn_novo_caso")).click();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
// Cod Os Estratégia VREL
String CodOs = driver.findElement(By.xpath("//*[#id=\"content\"]/div[1]/fieldset[1]/div[2]/div[3]/span"))
.getText();
// Campo Análise de Contrato
Select analiseContrato = new Select(driver.findElement(By.id("cboMotivo")));
analiseContrato.selectByIndex(5);
try {
// Campo Ação
Select acao = new Select(driver.findElement(By.id("cboSubMotivo")));
acao.selectByIndex(3);
// Status
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement ele = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("cboStatus")));
String valorStatus = ele.getText();
// driver.findElement(By.id("cboStatus")).getText();
Assert.assertEquals(" R", valorStatus);
// Chamado
driver.findElement(By.id("txtChamado")).sendKeys("Teste");
// Observação
driver.findElement(By.id("txtObservacao")).sendKeys("Teste 07/06/2018");
// Botão Salvar
driver.findElement(By.id("btnSalvar")).click();
} catch (StaleElementReferenceException e) {
// Campo Ação
Select acao = new Select(driver.findElement(By.id("cboSubMotivo")));
acao.selectByIndex(3);
// Status
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement ele = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("cboStatus")));
String valorStatus = ele.getText();
// String valorStatus = driver.findElement(By.id("cboStatus")).getText();
Assert.assertEquals(" R", valorStatus);
// Chamado
driver.findElement(By.id("txtChamado")).sendKeys("Teste");
// Observação
driver.findElement(By.id("txtObservacao")).sendKeys("Teste 07/06/2018");
// Botão Salvar
driver.findElement(By.id("btnSalvar")).click();
} catch (Exception e) {
// Campo Ação
Select acao = new Select(driver.findElement(By.id("cboSubMotivo")));
acao.selectByIndex(3);
// Status
String valorStatus = driver.findElement(By.id("cboStatus")).getText();
Assert.assertEquals(" R", valorStatus);
// Chamado
driver.findElement(By.id("txtChamado")).sendKeys("Teste");
// Observação
driver.findElement(By.id("txtObservacao")).sendKeys("Teste 07/06/2018");
// Botão Salvar
driver.findElement(By.id("btnSalvar")).click();
}
// Select na base para validar o status da NU_OS
ValidarEstrategiaPage p = new ValidarEstrategiaPage();
p.returnNuOs(CodOs);
// Saindo do Bko+
Thread.sleep(1000);
driver.findElement(By.linkText("Sair")).click();
}
#AfterClass
public static void closeBrowser() {
driver.quit();
}}

You clearly defined your WebDriver object as local variable inside the method:
#Test
public void validarStatusOs() throws InterruptedException {
WebDriver driver = new ChromeDriver();
In order for both the 'After' and 'Test' methods to interact with the global variable, change to:
#Test
public void validarStatusOs() throws InterruptedException {
driver = new ChromeDriver();
And BTW, change your class name from 'validarStatus' to 'ValidarStatus'. Starting a class name in upper case letter is a major best practice in Java.

As per your code block you have defined a global instance of WebDriver as:
private static WebDriver driver;
Within validarStatusOs() method you have initialized another local instance of WebDriver as:
WebDriver driver = new ChromeDriver();
When the control of your program comes out of validarStatusOs() method, the local instance of WebDriver is no more accessible.
So, when the control of your program enters within closeBrowser() method it tries to use the global instance of WebDriver and throws java.lang.NullPointerException.
Solution
As you have declared a global instance of WebDriver, use the same instance throughout your program. So you need to change the line:
WebDriver driver = new ChromeDriver();
To:
driver = new ChromeDriver();

Related

The button in date picker is getting clicked more than the number of clicks needed [duplicate]

I am trying to select a "Depart date" as of 31st october 2018 from the calender https://spicejet.com/ But I am getting error "unknown error: Element is not clickable at point (832, 242). Other element would receive the click: ..." Please help me out. Here is my code getting such exception:
public class bookflight extends Thread {
UtilityMethods utilObj= new UtilityMethods();
#Test
public void SighnUp() throws IOException
{
utilObj.getdriver().get("https://spicejet.com");
utilObj.getdriver().manage().window().maximize();
utilObj.getdriver().findElement(By.id("ctl00_mainContent_ddl_originStation1_CTXT")).click();
utilObj.getdriver().findElement(By.xpath("//a[contains(text(),'Guwahati (GAU)')]")).click();
utilObj.getdriver().findElement(By.xpath("//a[contains(text(),'Goa (GOI)')]")).click();
utilObj.getdriver().findElement(By.className("ui-datepicker-trigger")).click();
utilObj.getdriver().findElement(By.xpath("//div[#class='ui-datepicker-group ui-datepicker-group-first'])/parent:://table[#class='ui-datepicker-calendar']following-sibling::./a/contains(text(),'31')")).click();
}
}
To select From (e.g. Guwahati(GAU)), To (e.g. Goa(GOI)) destination and DEPART DATE as 31/10 within the url https://spicejet.com/ you need to induce WebDriverWait for the desired element to be clickable and you can use the following solution:
Code Block:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class spicejet_login {
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("https://spicejet.com");
new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//input[#id='ctl00_mainContent_ddl_originStation1_CTXT']"))).click();
driver.findElement(By.xpath("//div[#id='glsctl00_mainContent_ddl_originStation1_CTNR']//table[#id='citydropdown']//li/a[#value='GAU']")).click();
driver.findElement(By.xpath("//div[#id='ctl00_mainContent_ddl_destinationStation1_CTNR']//table[#id='citydropdown']//li/a[#value='GOI']")).click();
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//table[#class='ui-datepicker-calendar']//tr//a[contains(#class,'ui-state-default') and contains(.,'31')]"))).click();
}
}
Browser Snapshot:
There is lots of different factors which results into this exception,
i like to suggest you to try putting some wait.
WebDriverWait wait = new WebDriverWait(utilObj.getdriver(), 10);
wait.until(ExpectedConditions.elementToBeClickable(By.id("ctl00_mainContent_ddl_originStation1_CTXT")));
then try clicking element,
utilObj.getdriver().findElement(By.id("ctl00_mainContent_ddl_originStation1_CTXT")).click();
You can click on element by Action class, based on Exception type:
Actions action = new Actions(driver);
action.moveToElement(WebElement to click).click().perform();
Updated answer to click next date.
//div[contains(#class, 'ui-datepicker-group-first')]//td[#data-month='9' and #data-year='2018']/a[.=31]
You can modify the above XPATH to select date based on YEAR/MONTH/DATE. for more XPath creation go-through my answers.
var path ="//div[contains(#class, 'ui-datepicker-group-first')]//td[#data-month='9' and #data-year='2018']/a[.=31]";
var elem = document.evaluate(path, window.document, null, 9, null ).singleNodeValue;
console.log( elem );
elem.click();
When you enter FROM and TO data, then DEPART DATE field get auto selected. So, just you need to select the first data using javascript.
FROM « //div[#id='ctl00_mainContent_ddl_originStation1_CTNR']//a[#text='Guwahati (GAU)']
TO « //div[#id='ctl00_mainContent_ddl_destinationStation1_CTNR']//a[#text='Goa (GOI)']
DEPART DATE «
//div[contains(#class, 'ui-datepicker-group-first')]//a[contains(#class, 'ui-state-active')]
sample test program.
import io.github.yash777.driver.Browser;
import io.github.yash777.driver.Drivers;
import io.github.yash777.driver.WebDriverException;
public class SpiceJET {
static WebDriver driver;
static WebDriverWait explicitWait;
public static void main(String[] args) throws WebDriverException, IOException {
test();
}
public static void test() throws WebDriverException, IOException {
Drivers drivers = new Drivers();
String driverPath = drivers.getDriverPath(Browser.CHROME, 63, "");
System.setProperty(ChromeDriverService.CHROME_DRIVER_EXE_PROPERTY, driverPath);
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
driver = new ChromeDriver( capabilities );
explicitWait = new WebDriverWait(driver, 10);
//Maximize browser window
driver.manage().window().maximize();
//Go to URL which you want to navigate
driver.get("https://spicejet.com/");
clickElement("//input[#id='ctl00_mainContent_ddl_originStation1_CTXT'][1]");
clickElement("//div[#id='ctl00_mainContent_ddl_originStation1_CTNR']//a[#text='Guwahati (GAU)']");
clickElement("//input[#id='ctl00_mainContent_ddl_destinationStation1_CTXT'][1]");
clickElement("//div[#id='ctl00_mainContent_ddl_destinationStation1_CTNR']//a[#text='Goa (GOI)']");
clickUsingJavaScript("//div[contains(#class, 'ui-datepicker-group-first')]//a[contains(#class, 'ui-state-active')]");
}
}
public static void clickElement(String locator) {
By findBy = By.xpath( locator );
WebElement element = explicitWait.until(ExpectedConditions.elementToBeClickable( findBy ));
element.click();
}
public static void clickUsingJavaScript( String locator ) {
StringBuffer click = new StringBuffer();
click.append("var elem = document.evaluate(\""+locator+"\", window.document, null, 9, null ).singleNodeValue;");
click.append("elem.click();");
System.out.println("JavaScript Click.");
jse.executeScript( click.toString() );
}
For Automatic management of Selenium Driver Executable’s in run-time for Java use SeleniumWebDrivers
NOTE: If you are selecting DEPART DATE which got auto selected then selenium throws exception
Exception in thread "main" org.openqa.selenium.WebDriverException: unknown error:
Element <input type="text" readonly="readonly" id="ctl00_mainContent_view_date2" class="custom_date_pic required home-date-pick">
is not clickable at point (784, 241). Other element would receive the click: <span class="ui-datepicker-month">...</span>
I hope below code is helpful and handle departure and return date
public class SpicejetDropdowns1 {
public static void main(String[] args) throws InterruptedException
{ System.setProperty("webdriver.chrome.driver","E:\\ChromeDriver\\ChromeDriver2.46\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
driver.get("http://www.spicejet.com/");
driver.manage().window().maximize();
System.out.println(driver.getTitle()); driver.findElement(By.cssSelector("#ctl00_mainContent_rbtnl_Trip_1")).click();
// OriginStation
driver.findElement(By.xpath(".//*[#id='ctl00_mainContent_ddl_originStation1_CTXT']")).click();
driver.findElement(By.cssSelector("a[value='DEL']")).click();
// Destination
driver.findElement(By.xpath(".//*[#id='ctl00_mainContent_ddl_destinationStation1_CTXT']")).click();
driver.findElement(By.xpath("(//a[#value='HYD'])[2]")).click();
Thread.sleep(3000); driver.findElement(By.xpath("//input[#id='ctl00_mainContent_view_date1']")).click();
if(driver.findElement(By.id("Div1")).getAttribute("style").contains("1"))
{
System.out.println("its enabled");
Assert.assertTrue(true);
}
else
{
Assert.assertTrue(false);
}
while(!driver.findElement(By.cssSelector("div[class='ui-datepicker-title'] [class='ui-datepicker-month']")).getText().contains("October"))
{
// driver.findElement(By.xpath("//span[contains(text(),'Next')]")).click();
driver.findElement(By.xpath("//a[#class='ui-datepicker-next ui-corner-all']/span[#class='ui-icon ui-icon-circle-triangle-e']")).click();
System.out.println(driver.findElement(By.cssSelector("div[class='ui-datepicker-title'] [class='ui-datepicker-month']")).getText());
}
List<WebElement> dates= driver.findElements(By.xpath("//table[#class='ui-datepicker-calendar']//tr//td"));
int count= dates.size();
for(int i=0; i<count; i++)
{
String txt = driver.findElements(By.xpath("//table[#class='ui-datepicker-calendar']//tr//td")).get(i).getText();
if(txt.equalsIgnoreCase("28"))
{
driver.findElements(By.xpath("//table[#class='ui-datepicker-calendar']//tr//td")).get(i).click();
System.out.println(txt);
break;
}
}
// Return Date Selection
Thread.sleep(3000); driver.findElement(By.xpath("//input[#id='ctl00_mainContent_view_date2']")).click();
while(!driver.findElement(By.cssSelector("div[class='ui-datepicker-title'] [class='ui-datepicker-month']")).getText().contains("October"))
{
// driver.findElement(By.xpath("//span[contains(text(),'Next')]")).click();
driver.findElement(By.xpath("//a[#class='ui-datepicker-next ui-corner-all']/span[#class='ui-icon ui-icon-circle-triangle-e']")).click();
System.out.println(driver.findElement(By.cssSelector("div[class='ui-datepicker-title'] [class='ui-datepicker-month']")).getText());
}
List<WebElement> MDates= driver.findElements(By.xpath("//table[#class='ui-datepicker-calendar']//tr//td"));
int Mcount= dates.size();
for(int i=0; i<Mcount; i++)
{
String txt = driver.findElements(By.xpath("//table[#class='ui-datepicker-calendar']//tr//td")).get(i).getText();
if(txt.equalsIgnoreCase("31"))
{
driver.findElements(By.xpath("//table[#class='ui-datepicker-calendar']//tr//td")).get(i).click();
System.out.println(txt);
break;
}
}
//Select Passengers
Thread.sleep(4000);
driver.findElement(By.xpath(".//*[#id='divpaxinfo']")).click();
Thread.sleep(4000);
WebElement Adults = driver.findElement(By.xpath("//select[#id='ctl00_mainContent_ddl_Adult']")); Select adultsdrp = new Select(Adults);
adultsdrp.selectByValue("2");
WebElement childs = driver.findElement(By.xpath("//select[#id='ctl00_mainContent_ddl_Child']"));
Select childsdrp = new Select(childs);
childsdrp.selectByValue("2");
driver.findElement(By.xpath(".//*[#id='divpaxinfo']")).click();
System.out.println(driver.findElement(By.xpath(".//*[#id='divpaxinfo']")).getText());
//Static Currency Dropdown
WebElement Currency = driver.findElement(By.id("ctl00_mainContent_DropDownListCurrency"));
Select currencydrp = new Select(Currency);
currencydrp.selectByValue("USD"); Assert.assertEquals(driver.findElement(By.id("ctl00_mainContent_DropDownListCurrency")).getAttribute("value"), "USD"); System.out.println(driver.findElement(By.id("ctl00_mainContent_DropDownListCurrency")).getAttribute("value"));
}
}

appium issue, not able to launch two apps

I am not able to launch two apps in single script using APPIUM, it shows error as
An unknown server-side error occurred while processing the command.
The desired should not include both of an 'appPackage' and a
'browserName'
public class SalesLeadProg {
public AndroidDriver<MobileElement> driver;
private WebElement element; public WebDriverWait wait;
#BeforeMethod
public void setup() throws MalformedURLException, InterruptedException {
DOMConfigurator.configure("log4j.xml");
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability("automationName", "UiAutomator2");
caps.setCapability("deviceName", "sunil");
caps.setCapability("udid", "422b21e7");
caps.setCapability("platformName", "Android");
caps.setCapability("platformVersion", "8.1.0");
caps.setCapability("appPackage", "com.mfcwl.mfc_dealer");
caps.setCapability("appActivity", "com.mfcwl.mfc_dealer.Activity.SplashActivity");
caps.setCapability("autoAcceptAlerts", true);
caps.setCapability("noReset", true);
caps.setCapability("browserName", "Chrome");
//it will launch the chrome browser in mobile
caps.setCapability(ChromeOptions.CAPABILITY, true);
ChromeOptions options=new ChromeOptions();
options.setExperimentalOption("androidPackage", "com.android.chrome");
caps.setCapability("autoGrantPermissions", true);
// to allow the permission of contacts camara, which comes in the begining when we install the app*/
caps.setCapability("unicodeKeyboard", true);
// to make the keyboard working
caps.setCapability("resetKeyboard", false);
// it
try {
driver = new AndroidDriver<MobileElement> (new URL("0.0.0.0:4723/wd/hub"),caps);
}
catch(MalformedURLException e) {
System.out.println(e);
}
driver.manage().timeouts().implicitlyWait(16, TimeUnit.SECONDS);
}
#Test
public void Dashboard() throws MalformedURLException, InterruptedException {
MobileElement email = driver.findElement(By.id("com.mfcwl.mfc_dealer:id/email"));
Thread.sleep(4000);
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOf(email));
boolean isElementPresent = email.isDisplayed();
System.out.println(isElementPresent);
Thread.sleep(4000);
driver.findElementById("com.mfcwl.mfc_dealer:id/email").clear();
driver.findElementById("com.mfcwl.mfc_dealer:id/email").sendKeys("prestige");
System.out.println("Entered ID");
driver.findElement(By.id("com.mfcwl.mfc_dealer:id/password")).clear();
driver.findElement(By.id("com.mfcwl.mfc_dealer:id/password")).sendKeys("Mahindra");
System.out.println("Entered Password");
driver.findElement(By.id("com.mfcwl.mfc_dealer:id/email_sign_in_button")).click();
//to login
System.out.println("Signed IN");
Thread.sleep(4000);
driver.findElement(By.id("com.mfcwl.mfc_dealer:id/homeTab")).click();
//to click on home button
System.out.println("Clicked on Home button");
Thread.sleep(3000); //Set ChromeDriver location
System.setProperty("webdriver.chrome.driver", "./ChromeDriver/chromedriver.exe");
driver.get("google.com/"); driver.findElement(By.id("com.mfcwl.mfc_dealer:id/click_lead")).click();
System.out.println("Clicked on Today's follow up leads section");
driver.findElement(By.id("com.mfcwl.mfc_dealer:id/lead_filter")).click();
Please assist with a solution on how to use two apps in APPIUM with a single script.

UnReachableBrowserException in Chome with Selenium Webdriver

i am getting UnhandledBrowserException when trying the below code in chrome:
public class myClass {
public static void main(String[] args) {
// declaration and instantiation of objects/variables
System.setProperty("webdriver.chrome.driver", "C://Program Files (x86)//Google//Chrome//Application//chrome.exe");
WebDriver driver= new ChromeDriver();
String baseURL = "http://newtours.demoaut.com";
String expectedTitle = "Welcome: Mercury Tours";
String actualTitle = "";
// launch Firefox and direct it to the Base URL
driver.get(baseURL);
// get the actual value of the title
actualTitle = driver.getTitle();
/*
* compare the actual title of the page witht the expected one and print
* the result as "Passed" or "Failed"
*/
if (actualTitle.contentEquals(expectedTitle)){
System.out.println("Test Passed!");
} else {
System.out.println("Test Failed");
}
//close Firefox
driver.close();
// exit the program explicitly
System.exit(0);
}
It is launching a new session in chrome but then throwing the exception. Any help would be highly appreciated.
I think you are hitting program executable instead of driver.
System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
WebDriver driver = new ChromeDriver();
It should be path to chrome driver not the chrome browser executable.
You can take a look at https://sites.google.com/a/chromium.org/chromedriver/getting-started

To perform Autosuggestion using Selenium

Having some problem in selecting the option from the dropdown using autosuggestion. Please give a solution to select the option.
The related code is posted below :-
#Test(priority = 4)
public void ReportType() throws InterruptedException {
WebElement reporttype = driver.findElement(By.xpath("html/body/form/div[3]/table[2]/tbody/tr[3]/td/table/tbody/tr[1]/td/table/tbody/tr[2]/td/div/span/table/tbody/tr[3]/td[2]/span/table/tbody/tr/td[2]/input[1]"));
reporttype.clear();
reporttype.sendKeys("NMQ De");
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("html/body/form/div[3]/table[2]/tbody/tr[3]/td/table/tbody/tr[1]/td/table/tbody/tr[2]/td/div/span/table/tbody/tr[3]/td[2]/span/div/ul/li[4]")));
Thread.sleep(5000);
driver.findElement(By.xpath("html/body/form/div[3]/table[2]/tbody/tr[3]/td/table/tbody/tr[1]/td/table/tbody/tr[2]/td/div/span/table/tbody/tr[3]/td[2]/span/div/ul/li[4]")).click();
}
Think your question as google search when u do some google search google provides u auto suggestion
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
String textToSelect = "headlines today";
WebDriver driver = new FirefoxDriver();
driver.get("https://www.google.co.in/");
Thread.sleep(2000);
WebElement autoOptions= driver.findElement(By.id("lst-ib"));
autoOptions.sendKeys("he");
List<WebElement> optionsToSelect = driver.findElements(By.xpath("//div[#class='sbqs_c']"));
for(WebElement option : optionsToSelect){
System.out.println(option);
if(option.getText().equals(textToSelect)) {
System.out.println("Trying to select: "+textToSelect);
option.click();
break;
}
}

Selenium NullPointerException while trying to find WebElement

While trying to execute below method, I receive NullPointerException:
#Test
public static void test1() {
System.out.print("\nTo find UserName element");
WebElement element =driver.findElement(By.xpath("//input[#name='email']"));
WebElement element = driver.findElement(By.id("email"));
element.sendKeys("abhinav_shankar");
System.out.print("\nElement found");
System.out.print("\njunittest2 class-test1 executed before sleep");
Thread.sleep(15000);
System.out.print("\njunittest2 class-test1 executed after sleep");
}
Below excpetion is caught at line "WebElement element = driver.findElement(By.id("email"));"
Thread [main] (Suspended (exception NullPointerException))
Mytestclass.test1() line: 44
Mytestclass.main(String[]) line: 21
I tried using xpath as written in above code but it also gives same error.
EDIT:
#BeforeClass
public static void openbrowser() {
FirefoxProfile profile = new FirefoxProfile();
System.out.print("\nBrowser open");
WebDriver driver = new FirefoxDriver(profile);
driver.manage().window().maximize();
driver.get("website-url");
}
You seem to have declared driver twice. One in the class level & one in openbrowser method.
You initialize only the openbrowser driver. The class level driver is still null. So, test1 method throws null pointer exception.
So, remove the driver re-declaration in the openbrowser method. Just this will do and it should work.
driver = new FirefoxDriver(profile);
Issue :
Your Webdriver objects seems to be declared inside a function openbrowser() because of which driver object scope is only with that function. So the NullpointerException could be due to driver.
Solution :
Declare the Webdriver globally and initialize within the openbrowser method so that you can use it in test1() or any other methods also.
public WebDriver driver;
#BeforeClass
public static void openbrowser() {
//Driver initialization
}
#Test
public static void test1() {
//Use the driver here
}