org.openqa.selenium.SessionNotCreatedException: desired capabilities error - java

After the attempt to run my tests I always get:
org.openqa.selenium.SessionNotCreatedException: Unable to create new remote session. desired capabilities = Capabilities [{platformVersion=6.0, platformName=Android, deviceName=Xiomi}], required capabilities = Capabilities [{}]
java-client 5.0.0 beta 9
Appium 1.6.5
selenium standalone 3.4.0
Using Android Studio
public class MyTest1 {
AppiumDriver driver;
#Before
public void setUp() throws Exception {
DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
desiredCapabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
desiredCapabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "Xiomi");
desiredCapabilities.setCapability(MobileCapabilityType.PLATFORM_VERSION, "6.0");
URL url = new URL("http://127.0.0.1:4723/wd/hub");
driver = new AppiumDriver(url, desiredCapabilities);
}
#After
public void tearDown() throws Exception {
driver.quit();
}
#Test
public void Test(){
}
}
Please help! I can't understand what am I doing wrong

I've added next desired capabilities and exception stopped occurring:
desiredCapabilities.setCapability("appPackage", "com.package.app");
desiredCapabilities.setCapability("appActivity", ".activities.App");
// Add real app package and activity instead of mine.

Related

How to resolve the issue with the browser is being closed when start running testng using Edge driver

I have a couple issues when I run the test with edge web browser.
The first issue is that every time I run the test by run as > testng test , the automation will close all the existing open edge web browser and then it will open a brand new edge browser but it is blank (could not load the url)
The second issue is that the automation would only load the url if I manually close all of the existing edge browser.
Is there a way to fix these issues? please help me thank you in advance.
Here is my code
public class OpenThePageUsingEdge {
public String baseUrl = "https://catalog-qa.baylorgenetics.com/search";
String driverPath = "C:\\Eclipse\\MicrosoftWebDriver.exe";
public WebDriver driver ;
//initiate NameofInsured as the GenerateData class
//GenerateData NameOfAddrecipients;
#BeforeTest
public void setup(ITestContext ctx) {
TestRunner runner = (TestRunner) ctx;
runner.setOutputDirectory("J:\\zzQA Selenium Automation Suite\\Test Results");
}
#Test
public void openThePageusingEdge () throws InterruptedException {
System.out.println("launching Edge browser");
System.setProperty("webdriver.edge.driver", driverPath);
driver = new EdgeDriver();
driver.get(baseUrl);
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

Appium: Connect to 127.0.0.1:4723 [/127.0.0.1] failed: Connection refused: connect

I am trying to write my first Java Appium test.
Each time I'm running my code the following errors are presented:
org.openqa.selenium.WebDriverException: org.apache.http.conn.HttpHostConnectException: Connect to 127.0.0.1:4723 [/127.0.0.1] failed: Connection refused: connect
....
at MyFirstTest.setupTest(MyFirstTest.java:52)
Caused by: org.apache.http.conn.HttpHostConnectException: Connect to 127.0.0.1:4723 [/127.0.0.1] failed: Connection refused: connect
at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:158)
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:353)
Here is my code:
public class MyFirstTest
{
public AppiumDriver<MobileElement> driver;
public WebDriverWait wait;
String usersEmail = "xxxx#gmail.com";
String userPassword = "aaaaaa";
public void login() throws InterruptedException
{
WebElement email = driver.findElement(By.xpath("//android.widget.EditText[contains(#text,'Enter Email')]"));
email.sendKeys(usersEmail);
Sleeper.SYSTEM_SLEEPER.sleep(new Duration(1000, TimeUnit.MILLISECONDS));
driver.findElement(By.xpath("//android.widget.RelativeLayout[contains(#resource-id,'edtPassword')]//android.widget.EditText[contains(#resource-id,'edtText')]")).sendKeys(userPassword);
driver.findElement(By.xpath("//android.widget.Button[contains(#resource-id,'btnLogin')]")).click();
}
#Before
public void setupTest() throws Exception
{
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("platormName", "Android");
capabilities.setCapability("deviceName", "Redmi");
capabilities.setCapability("appPackage","com.ls.stockpair");
capabilities.setCapability("appActivity","com.ls.stockpair.activity.SplashActivity");
driver = new AndroidDriver<MobileElement>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
Thread.sleep(8000);
}
#Test
public void go() throws InterruptedException
{
WebElement email = driver.findElement(By.xpath("//android.widget.EditText[contains(#text,'Enter Email')]"));
email.sendKeys(usersEmail);
Sleeper.SYSTEM_SLEEPER.sleep(new Duration(1000, TimeUnit.MILLISECONDS));
driver.findElement(By.xpath("//android.widget.RelativeLayout[contains(#resource-id,'edtPassword')]//android.widget.EditText[contains(#resource-id,'edtText')]")).sendKeys(userPassword);
driver.findElement(By.xpath("//android.widget.Button[contains(#resource-id,'btnLogin')]")).click();
Sleeper.SYSTEM_SLEEPER.sleep(new Duration(2000, TimeUnit.MILLISECONDS));
}
}
Currently I'm starting Appium manually before running the code. Also tried to start Appium from the code - this did not help.
This code worked in the first day I started writing this code however somehow (I really do not know why) this stopped working.
I saw many questions with different answers about this issue in the web but no one helped me so far.
I'm working with a real Android device connected via USB to my Windows machine.
Below is the best solution, You don't need to start appium manually everytime.
public static AndroidDriver<MobileElement> driver=null;
public static AppiumDriverLocalService service=null;
service = AppiumDriverLocalService.buildDefaultService();
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("platormName", "Android");
capabilities.setCapability("deviceName", "Redmi");
capabilities.setCapability("appPackage","com.ls.stockpair");
capabilities.setCapability("appActivity","com.ls.stockpair.activity.SplashActivity");
service.start();
driver = new AndroidDriver<MobileElement>(service, capabilities);
Note : If you are using appium version 1.5.3+ then you need to set APPIUM_BINARY_PATH in environmental variable.
e.g. APPIUM_BINARY_PATH=C:\Users\YourUserName\AppData\Roaming\npm\node_modules\appium\build\lib\main.js
For more details on how to set APPIUM_BINARY_PATH please refer below screen shot:

Appium startActivity() Function

I am new to Appium, In my code I have given required desired capabilities and wrote one test case that is working fine. Now I want to launch another App for second test in same code , how can I do that ?
I heard about startActivity(app-package,app Activity) but its not working, it says startActivity() not defined for Web Driver .
public class Calculator {
WebDriver driver;
#BeforeClass
public void setUp() throws MalformedURLException{
//Set up desired capabilities and pass the Android app-activity and app-package to Appium
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.BROWSER_NAME, "Android");
capabilities.setCapability(CapabilityType.VERSION, "4.4");
capabilities.setCapability("platformName", "Android");
capabilities.setCapability("deviceName", "14085521650378");
capabilities.setCapability("appPackage", "com.android.calculator2"); // This is package name of your app (you can get it from apk info app)
capabilities.setCapability("appActivity","com.android.calculator2.Calculator");
configurations specified in Desired Capabilities
driver = new RemoteWebDriver(new URL("http://127.0.0.1:9515/wd/hub"), capabilities);
}
#Test
public void testCal(){
driver.findElement(By.name("2")).click();
driver.findElement(By.name("+")).click();
driver.findElement(By.name("4")).click();
driver.findElement(By.name("=")).click();
}
#Test
public void Test2() { driver.startActivity("appPackage", "com.tttk.apc","appActivity","com.tttk.apc.DWDemoActivity");
for(int i=0; i<20;i++)
driver.findElement(By.className("android.widget.ImageButton")).click();
}
#AfterClass
public void teardown(){
//close the app
driver.quit();
}}
Seems like you are trying to use the method with a WebDriver instance.
The startActivity method is provided by an interface StartsActivity implemented by AndroidDriver only. So ideally this shall work :
((AndroidDriver) driver).startActivity(<appPackage>, <appActivity>);
public static void start() {
try {
((AndroidDriver) driver).startActivity("com.example.test", "com.example.LaunchApp");
} catch (Exception e) {
e.printStackTrace();
}
}
You have to enter your app package name and activity name to maximize the app.

Appium and Eclipse

I am new to Appium and I have installed Appium on ubuntu and make my first test using java command in an Eclipse while running my test I get an error: A new session could not be created.permission to start activity denied.
please Advice.
Here is my code:
public class BoxerTest {
AndroidDriver dr;
#Test
public void setUp() throws MalformedURLException{
DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability("deviceName","TA09402ERN");
cap.setCapability("platformVersion","5.0.2");
cap.setCapability("platformAndroid","Android");
cap.setCapability("appPackage","com.boxer.browser");
cap.setCapability("appActivity","com.boxer.browser.MainActivity");
dr = new AndroidDriver(new URL ("http://0.0.0.0:4723/wd/hub"), cap);
}
#After
public void End(){
dr.quit();
}
}
Launchable app activity might be wrong. To get the launchable activity, take the APK and run the command aapt dump badging <boxer.apk>

Not able to run selenium script using phantomjs driver

I am new to phantomjs driver, I need to run my script in background using phantomjs headless driver.
Here is my code i am getting null-pointer exception.
currently am using selenium 2.32,testNG,phantomjs jar 1.0.3
public class PhantomjsDemo {
public WebDriver driver;
#BeforeMethod
public void setup(){
DesiredCapabilities caps = new DesiredCapabilities();
caps.setJavascriptEnabled(true);
caps.setCapability("takesScreenshot", true);
caps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,"C:\\phantomjs-1.9.2-windows\\phantomjs.exe");
WebDriver driver = new PhantomJSDriver(caps);
driver.get("www.google.com");
}
#Test
public void google(){
driver.findElement(By.xpath("//*[#id='gbqfba']")).getText();
driver.findElement(By.xpath("//*[#id='gbqfba']")).getSize().getHeight();
driver.findElement(By.xpath("//*[#id='gbqfba']")).getSize().getWidth();
driver.findElement(By.xpath("//*[#id='gbqfba']")).click();
}
#AfterMethod
public void close(){
driver.quit();
}
}
You are not initializing your Webdriver member variable in the setup() method, but a method variable:
WebDriver driver = new PhantomJSDriver(caps);
Change it to
this.driver = new PhantomJSDriver(caps);
and the NPE should go away.

Categories

Resources