I'm automating a php web page using webdriver and java language. My code was executing and I was able to perform actions on web page, but today only the login method is executing and my second method gets failed everytime I run. I'm so worried why it is happening. Please help, I'm new in automation testing.
public class TestNGClass {
private String baseUrl = "http://test.com/test2/1.4.6.3/public/admin/";
private WebDriver driver = new FirefoxDriver();
#Test
public void login() {
driver.manage().window().maximize();
driver.get(baseUrl);
driver.findElement(By.name("username")).sendKeys("abc");
driver.findElement(By.name("password")).sendKeys("123");
driver.findElement(By.name("login")).submit();
System.out.print("\nCongrats..You have successfully logged in.");
}
#Test
public void createUser() {
String expectedTitle = "User";
String actualTitle = driver.getTitle();
Assert.assertEquals(actualTitle, expectedTitle,"Title Not Found!");
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.findElement(By.xpath("//body/div[3]/div[2]/ul/li[2]/a/img")).click();
Error :
java.lang.AssertionError: Title Not Found! expected [User] but found []
This is because you are using Assert.
Assert.assertEquals(actualTitle, expectedTitle,"Title Not Found!");
Make use of Try catch in-order to proceed next step.
try{
Assert.assertEquals(actualTitle, expectedTitle,"Title Not Found!");
}catch (Exception e)
{
}
Related
I am trying to test if the URL of the window is correct after clicking on a link. But unexpected windows with URL data:, get open between the test and getCurrentUrl grabs the "data:," as the URL and fails the test instead of the actual URL.
The windows with data:, is open even after all the test is complete.
Feature steps:
public void homePageOpens() {
WebDriverWait wait = new WebDriverWait(driver, 15);
wait.until(ExpectedConditions.titleContains("STORE"));
String homepageUrl = navigationUser.getUrl();
System.out.println(homepageUrl);
Assert.assertTrue(homepageUrl.contains("https://www.example.com/index.html"));
driver.close();
}
Navigation steps:
#Step("Get the URL")
public String getUrl() { return basePage.getUrl();
}
BasePage:
public String getUrl() {
System.out.println("just testing");
WebDriver driver = new ChromeDriver();
return driver.getCurrentUrl();
}
Replacing base page code with the following worked:
WebDriver driver = new ChromeDriver();
return driver.getCurrentUrl();
to
return getDriver().getCurrentUrl();
I am very new to selenium UI automation and I am trying my hands on with a simple application. I am using java with testNG. I need to integrate this test with CI and the test environment url will be different with every deployment. I am passing this as a parameter, difference between test and prod environment is that in the test, there won't be any login screen hence there is no need for authentication but my test verifies login and a login method. I need to be able to skip this test based on the URL supplied. Here is my code and the problem is testNG always suggests that the test was skipped but I can see it executing the login method. Please help me correct or understand what mistake I am committing.
public class Login {
WebDriver driver;
//Parameter - test environment URL
String url = System.getProperty("environment");
#Test (priority = 1)
public void verifyLogin() {
//Skip verifyLogin test in non prod environments
System.setProperty("webdriver.chrome.driver", "C://chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get(url);
if (url=="www.google.com")
//If url matches production url then execute the test, if url doesn't match production URL then skip login test as there won't be any authentication/login in non prod
{
LoginPage login = new LoginPage(driver);
login.signIn();
Assert.assertEquals(driver.getTitle(), "Application Name", "Login failed,");
String title = driver.getTitle();
System.out.println("Page title is " + title);
driver.close();
}
else if (url!="www.google.com"){
throw new SkipException("Skipping this test");
}
}
#Test(priority = 2)
public void userKey() {
System.setProperty("webdriver.chrome.driver", "C://chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get(url);
//If URL equals prod then call the login method to be able to login to the app else just execute the userKey method without having the need to login
if (url=="www.google.com");
{
LoginPage login = new LoginPage(driver);
login.signIn();
}
AccountManagementPage userKey = new AccountManagementPage(driver);
userKey.key();
driver.close();
}
}
The very exact use case is well explained here without throwing SkipException.
The idea is to create a custom annotation for the methods to be skipped & read the methods using IMethodInterceptor - decide to execute or not behind the scenes.
Updated for the question in the comment section:
You do not have to worry about 'TestEnvironment' or 'TestParameters' class.Just directly use the production check logic here.
Predicate<IMethodInstance> isTestExecutingOnProduction = (tip) -> {
return system.getProperty("env").
.equals("<production check here>");
};
I trying to automate a hybrid app using the appium. i had completed the total setup ready and also testing using sample apk file. I facing problem in getting the object properties for my hybrid app. I am not able to inspect ids using Appium inspector or uiautomatorviewer. It shows only one class for my app .
i also need to enable WebView debugging, for making
setWebContentsDebugging Enabled
to true on the WebView class.can any one help me how to do that?
some of the blogs are saying to keep driver.context("web_view"); but i not clear how to get that. please help to to solve this.
thanks.
this is my java class
public class myMavenTest {
private WebDriver driver;
//i think is not the way to do this.so i comented this.
/*public void onCreate(){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
if(0 != (getApplicationInfo().flags = ApplicationInfo.FLAG_DEBUGGABLE)){
WebView.setWebContentsDebuggingEnabled(true);
}
}
}*/
#BeforeTest
public void setUp() throws Exception
{
System.out.println("in the setup function");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
capabilities.setCapability("deviceName","My Android");
capabilities.setCapability("platformVersion","5.1");
capabilities.setCapability("platformName","Android");
capabilities.setCapability("appPackage","com.mysoftware.testapp");
capabilities.setCapability("appActivity","com.mysoftware.testapp.MainActivity");
try
{
driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
driver.manage().timeouts().implicitlyWait(5,TimeUnit.SECONDS);
//Thread.sleep(10000);
}
catch(MalformedURLException e)
{
e.printStackTrace();
}
}
#Test
public void Loginforsample() throws Exception
{
System.out.println("in the login() function");
//i tried using classname of my app. but it is not recognizing
driver.findElement(By.className("ink-dark")).click();
//After the button clicks need to enter the text
driver.findElement(By.xpath("//need to find xpath'")).sendKeys("My first Automation");
//tried using selendroid.apk works fine here.
/*driver.findElement(By.id("io.selendroid.testapp:id/startUserRegistration")).click();*/
Thread.sleep(10000);
}
#AfterTest
public void tearDown() throws InterruptedException
{
Thread.sleep(10000);
driver.quit();
}
}
Use below the sample Hybrid App code which I have written for a combination of WEBView Native View. Hope it helps for you!
public class Hybrid_App {
public static void main(String[] args) throws MalformedURLException, InterruptedException{
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("deviceName", "Atom 2x");
capabilities.setCapability("platformName", "Android");
capabilities.setCapability("platformVersion", "5.1");
capabilities.setCapability("appPackage","***YourHydridAppPkg****");
capabilities.setCapability("appActivity", "****YourlauchableActivity***");
AndroidDriver driver= new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
Thread.sleep(4000);
Set<String> contextHandles = driver.getContextHandles();
Map<String,String> hashMap= new HashMap<String,String>();
for(String contextname:contextHandles){
if(contextname.contains("NATIVE")){
hashMap.put("native", contextname);
}else{
hashMap.put("webview", contextname);
}
}
//native page - Native
driver.context(hashMap.get("native"));
WebDriverWait wait= new WebDriverWait(driver, 50);
WebElement ele_voucher = wait.until(ExpectedConditions.visibilityOfElementLocated(
By.xpath("//*[#class='android.view.View'][#index='9'][#content-desc='VOUCHERS']")));
System.out.println(ele_voucher.isDisplayed());
Thread.sleep(6000);
ele_voucher.click();
Thread.sleep(9000);
//second page - Native
driver.context(hashMap.get("native"));
Thread.sleep(5000);
driver.findElementByXPath("//*[#class='android.view.View'][#index='17'][#content-desc='REDEEM']").click();
Thread.sleep(8000);
System.out.println("----Third page----"+" uname,pwd element");
//third page - Webview
driver.context(hashMap.get("webview"));
Thread.sleep(6000);
driver.findElementByXPath("//input[#class='x-input-email'][#type='email'][#name='email']").sendKeys("descbatch#gmail.com");
WebElement ele_pwd = driver.findElementByXPath("//input[#class='x-input-password'][#type='password'][#name='password']");
ele_pwd.click();
Thread.sleep(4000);
ele_pwd.sendKeys("12345");
Thread.sleep(6000);
System.out.println("----Third page----"+" Sign in element");
//fourth page - Native
driver.context(hashMap.get("native"));
Thread.sleep(6000);
driver.findElementByXPath("//*[#class='android.view.View'][#index='69'][#content-desc='SIGN IN']").click();
Thread.sleep(6000);
driver.sendKeyEvent(AndroidKeyCode.BACK);
}
}
Here is my problem.
After executing testNG script , I'm getting error stating
org.testng.TestNGException:
Cannot instantiate class TestNG.DemoTestNG. Can anybody please give me the solution for my problem.
public class DemoTestNG {
WebDriver driver = new FirefoxDriver();
String appUrl = "http://192.168.1.116:9090/soxaudit/login";
#Test
public void Sox_Login() {
driver.get(appUrl);
driver.manage().window().maximize();
String expectedTitle = "SOX ADMIN - LOGIN";
String actualTitle = driver.getTitle();
Assert.assertEquals(expectedTitle,actualTitle);
WebElement username = driver.findElement(By.id("j_username"));
username.clear();
username.sendKeys(new String[]{"sox"});
WebElement password = driver.findElement(By.id("j_password"));
password.clear();
password.sendKeys(new String[]{"welcome#123"});
WebElement SignInButton = driver.findElement(By.xpath("//*[#id='login-page']/div/form/div/button"));
SignInButton.click();
// close the web browser
driver.close();
System.out.println("Test script executed successfully.");
// terminate the program
System.exit(0);
}
}
Hello i really need help with Selenium WebDriver using TestNG and Excel
i try to get data from excel to open browser and navigate URL. its work successfully and terminal and testng report showing test pass but its not open browser or doing anything its just run its self and show report
Config File
public void openBrowser(String browser){
try {
if (browser.equals("Mozilla")) {
driver = new FirefoxDriver();
} else if(browser.equals("IE")){
driver = new InternetExplorerDriver();
} else if(browser.equals("Chrome")){
System.setProperty("webdriver.chrome.driver", "\\Applications\\Google Chrome.app\\Contents\\MacOS\\Google Chrome ");
driver = new ChromeDriver();
}
} catch (Exception e) {
}
}
public void navigate(String baseUrl){
try {
driver.manage().window().maximize();
driver.manage().deleteAllCookies();
driver.navigate().to(baseUrl);
} catch (Exception e) {
}
}
And Test Execute File
public class NewTest {
public String exPath = Config.filePath;
public String exName = Config.fileName;
public String exWrSheet = "Logiin Functional Test";
public Config config;
#BeforeTest
public void openBrowser() {
config = new Config();
Excel.setExcelFile(exPath+exName, exWrSheet);
String browser = Excel.getCellData(1, 2);
config.openBrowser(browser);
}
#BeforeMethod
public void navigate() {
config = new Config();
Excel.setExcelFile(exPath+exName, exWrSheet);
String baseUrl = Excel.getCellData(2, 2);
config.navigate(baseUrl);
}
#Test
public void test() {
System.out.println("Test");
}
#AfterTest
public void closeBroser() {
//Config.tearDown();
}
I don't have quite enough rep to make a comment, which I would prefer here, but if you aren't getting any sort of exception, I'd suspect the value you're getting for the browser variable is not matching anything in your if-then-else in openBrowser and then falling through. Step through the code with a debugger or just add:
String browser = Excel.getCellData(1, 2);
System.out.println("Browser value from Excel =" + browser);
config.openBrowser(browser);
to see what you're reading from the file.
1 - TestNg is always pass because you are using "void" method and you catch "all" exception
2 - No browser opened because in openBrowser(String browser), NullPointException throws and you already catch it.
-> you need to init an instance of WebDriver and pass it through your test.