Android Espresso Turn OFF Cellular Data on Android Emulator through Runtime() not working - android-testing

I am writing instrumentation test for a scenario where data is not available on android emulator (api 16 and api 23). I tried the following code to disable data,
#Test
public void disconnectData() throws IOException {
String line;
Process p = Runtime.getRuntime().exec("svc data disable");
BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line = in.readLine()) != null) {
System.out.println(line);
Log.d("asdf", line);
}
in.close();
}
It does not do anything. Data is not disabled. It simply passed without error. There is no output.
If I run the same command from terminal using adb, it works:
adb -s emulator-5554 shell
svc data disable

Alternative solution is to use mockwebserver. E.g.,
server.enqueue(new MockResponse()
.setResponseCode(HttpURLConnection.HTTP_INTERNAL_ERROR)
.setBody(getStringFromFile(getInstrumentation().getContext(), "login.json")));

Related

How to start Edge browser in Incognito mode using selenium remote webdriver?

Currently we are working on selenium (2.53.0) with Edge browser using C#.
Edge browser stores cache information at 'localAppdata' folder because of cache, we are facing some issues while test cases execution.
I try to delete all cookies information using selenium (DeleteAllCookies) but it not working for Edge browser.
I read couple of Microsoft forums only way to skip cache, when we start Edge browser on incognito mode.
Can any one suggest how to start Edge browser instance in private (incognito mode) using selenium remote-webdriver
if you want to open Edge in Private (Incognito) mode, you can use this C# code:
EdgeOptions options = new EdgeOptions();
options.AddAdditionalCapability("InPrivate", true);
this.edgeDriver = new EdgeDriver(options);
Here is an example of what I use when setting up an EdgeDriver instance. (C#)
private IWebDriver SetupEdgeWebDriver(bool runHeadlessOnPipeline, int implicitWait = 12500)
{
IWebDriver webDriverInstance;
EdgeOptions edgeOptions = new EdgeOptions
{
//Microsoft Edge (Chromium)
UseChromium = true
};
if (EnableIncognito)
{
edgeOptions.AddArgument("inprivate");
}
edgeOptions.BinaryLocation = "C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedge.exe";
//azure devops pipeline
if (PipelineRun)
{
edgeOptions.AddArgument("disable-gpu");
edgeOptions.AddArgument("window-size=1920,960");
if (runHeadlessOnPipeline)
{
edgeOptions.AddArgument("headless");
}
}
//running on your local machine
else
{
edgeOptions.AddArgument("start-maximized");
}
edgeOptions.SetLoggingPreference(LogType.Driver, LogLevel.Debug);
webDriverInstance = new EdgeDriver(edgeOptions);
webDriverInstance.Manage().Timeouts().ImplicitWait = TimeSpan.FromMilliseconds(implicitWait);
return webDriverInstance;
}
This is the code I'm using with Selenium.WebDriver 4.0.0 and C# dotnet 5.0
EdgeOptions options = new();
options.AddArguments("InPrivate");
driver = new EdgeDriver(options);

Is it possible to spin another process from within an iOS application?

In others words reaching out to the command line and running another command while capturing the standard output.
The reading that've been doing so far seems to indicate that this is a clear violation of the sandbox model and therefore not possible.
You can easily do this is in Android:
//This is just an example don't get hanged up on the actual command.
Process process = Runtime.getRuntime().exec("cat somefile.txt");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = bufferedReader.readLine()) != null) {
logCatTraces.append(line);
logCatTraces.append("\r\n");
}

Open exe as service without "Interactive Service Detection" message in window 7

I have a simple application that watches a folder for any changes as following:
private void Form1_Load(object sender, EventArgs e)
> {
> FileSystemWatcher w = new FileSystemWatcher();
> w.Path = #"C:\temp";
> w.Changed += new FileSystemEventHandler(OnChanged);
> w.Created += new FileSystemEventHandler(OnChanged);
> w.Deleted += new FileSystemEventHandler(OnChanged);
> w.Renamed += new RenamedEventHandler(OnChanged);
> // Begin watching.
> w.EnableRaisingEvents = true;
}
// Define the event handlers.
private static void OnChanged(object source, FileSystemEventArgs e)
{
// Specify what is done when a file is changed, created, or deleted.
MessageBox.Show("File: " + e.FullPath + " " + e.ChangeType + Path.GetFileName(e.FullPath));
}
I added the same as service from the command prompt as
sc create <service name> binPath= <path of the exe file>
This added the exe in the services and also made the entries in Registry. But when I tried to start the service as
sc start <service name>
it showed up the "Interactive Service Detection" message.
I want to avoid this message from popping up and start the service.
I also need this to be done in c# but if anyone has any idea about doing it in cmd I can add it as a batch file and execute the same.
EDIT I
As #Seva suggested I created a service that calls the exe that I wish. I wrote the following code to start the exe on start of the service:
protected override void OnStart(string[] args)
{
base.OnStart(args);
BackgroundWorker bw = new BackgroundWorker();
bw.DoWork += new DoWorkEventHandler(bw_DoWork);
bw.RunWorkerAsync();
}
private void bw_DoWork(object sender, DoWorkEventArgs e)
{
p.StartInfo.CreateNoWindow = false;
p.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
p.StartInfo.WorkingDirectory = #"<my exe path>";
p.StartInfo.FileName = "<myexe.exe>";
p.StartInfo.Arguments = #"<my exe path>";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.Start();
p.WaitForExit();
base.Stop();
}
I installed the service successfully but is not starting the exe on starting.
EDIT II
The exe started. The service's property had to be configured to allow service interaction with desktop, but then again the "Interactive service detection" message is coming up.
You will have to rearchitecture your windows service into two parts -- a GUI-less service process and a separate UI app that runs on user desktop. There are many ways service can communicate with UI app. These SO questions will get you started:
GUI and windows service communication
Communication between windows service and desktop app
There is no other way around. BTW, your existing approach is already broken -- for Non-admin users and for remote desktop sessions -- they won't see UI from a service even if they want to.

SharpSSh: RunCommand in SshExec is always returning an empty string

When Using SharpSSh and the SshExec class, I can't get the RunCommand to work, it always returns an empty string. When I debug the SharpSsh library it returns -1 when it tries to read the command from a stream. It works when I use the sftp class in the same library, but that class doesn't support all the ftp commands I need.
Here is a standard example, I can't get this to produce a correct result either
SshConnectionInfo input = Util.GetInput();
SshExec exec = new SshExec(input.Host, input.User);
if(input.Pass != null) exec.Password = input.Pass;
if(input.IdentityFile != null) exec.AddIdentityFile( input.IdentityFile );
Console.Write("Connecting...");
exec.Connect();
Console.WriteLine("OK");
while(true)
{
Console.Write("Enter a command to execute ['Enter' to cancel]: ");
string command = Console.ReadLine();
if(command=="")break;
string output = exec.RunCommand(command);
Console.WriteLine(output);
}
Console.Write("Disconnecting...");
exec.Close();
Console.WriteLine("OK");
Any ideas on how I can get the RunCommand function to run some commands?
Thanks for any help :)
To get the standard output and error streams from .RunCommand,
I'll repeat the answer I posted to: SharpSSH - SSHExec, run command, and wait 5 seconds for data!
You may want to try the following overload:
SshExec exec = new SshExec("192.168.0.1", "admin", "haha");
exec.Connect();
string stdOut = null;
string stdError = null;
exec.RunCommand("interface wireless scan wlan1 duration=5", ref stdOut, ref stdError);
Console.WriteLine(stdOut);
exec.Close();
If their API does what the name implies, it should put the standard output of your command in stdOut and the standard error in stdError.
For more information about standard streams, check this out: http://en.wikipedia.org/wiki/Standard_streams

How do I read a multiline value using the Ant 'input' task?

Anyone know how I can enter a multiline value in an Ant script? I'm prompting the user for a Subversion commit comment using the input task, and I'd like to be able to support multiple lines of text.
I'm running the standalone version of Ant at the Windows command prompt.
I thought I might be able to do a search and replace for \n, but I can't see any easy way to do a replace from property value to property value in Ant. It looks like I'd have to write a file, replace in the file, and then load the file into another property. I don't want it that badly.
I'm not 100% positive about this, but I took a look at the Ant source code, and it just does a readLine():
From /org/apache/tools/ant/input/DefaultInputHandler.java:
/**
* Prompts and requests input. May loop until a valid input has
* been entered.
* #param request the request to handle
* #throws BuildException if not possible to read from console
*/
public void handleInput(InputRequest request) throws BuildException {
String prompt = getPrompt(request);
BufferedReader r = null;
try {
r = new BufferedReader(new InputStreamReader(getInputStream()));
do {
System.err.println(prompt);
System.err.flush();
try {
String input = r.readLine();
request.setInput(input);
} catch (IOException e) {
throw new BuildException("Failed to read input from"
+ " Console.", e);
}
} while (!request.isInputValid());
} finally {
if (r != null) {
try {
r.close();
} catch (IOException e) {
throw new BuildException("Failed to close input.", e);
}
}
}
}
Here is what I would do if I were you:
If you are using Ant 1.7, then try implementing your own InputHandler, as described in the documentation. The Apache License permits you to basically copy-and-paste the above code as a starting point.
If you are using Ant 1.6 or earlier, then just create your own MultiLineInput task. You can extend the existing Input class and just read multiple lines.
In either case, you would need to decide how the user indicates "I'm done." You could use a blank line or a period or something.
Good luck!
P.S. When I did a Google search for "ant multi-line input", this page was the first hit :-). Pretty impressive for a question that was asked less than an hour ago.

Resources