I have a problem when coding C# with Web Browser use Gecko library.
My code here:
public Form1()
{
InitializeComponent();
Gecko.Xpcom.Initialize(Application.StartupPath + "\\xulrunner");
string link = "http://google.com/";
geckoWebBrowser1.Navigate(link);
System.Threading.Thread.Sleep(10000);
Environment.Exit(0);
}
I want to when run my application, its will show my web browser with Google content and then automatic exit after 10 seconds.
Thanks.
Look your example, you have a very fast pc and when run app;
1- InitializeComponent 1 ms passed
2- Gecko.Xpcom.Initialize 1 ms passed
3- string link 1 ms passed
4- geckoWebBrowser1.Navigate(link) 1 ms passed (but maybe page load 20.000 ms)
5- Thread.Sleep(10000); 10.000 ms sleep
and exit.
Now you should Document complete event and maybe usable timer, because all when thread sleep is your solution won't work.
public Form1()
{
InitializeComponent();
Gecko.Xpcom.Initialize(Application.StartupPath + "\\xulrunner");
string link = "http://google.com/";
geckoWebBrowser1.Navigate(link);
geckoWebBrowser1.DocumentCompleted += geckoWebBrowser1_DocumentCompleted;
}
private void geckoWebBrowser1_DocumentCompleted(object sender, Gecko.Events.GeckoDocumentCompletedEventArgs e)
{
timer1.start();
}
Last one, count with timer, and application.exit.
Related
I am working on Selenium webdriver and I have write a function for window handling. I have written code for naukri.com popup handling. My scenario is to Open the naukri.com and without closing popup window. I want to switch main window and click on Login button.I have written the code and created a function. when I am running the script focus is going on main page and url is displayed as selected but I am not able to click on Login button. I am not understanding where the problem is.Please suggest me.
public static WebDriver fn_SetFocus_According_Title(WebDriver dObj, String arg_title)
{
Set<String> setcol_windowHandle=dObj.getWindowHandles();
Iterator<String>itcol_handleval=setcol_windowHandle.iterator();
while(itcol_handleval.hasNext()==true){
String windowhanldval=itcol_handleval.next();
dObj=dObj.switchTo().window(windowhanldval);
String apptitle=dObj.getTitle();
if(apptitle.contains(arg_title))
{
dObj=dObj.switchTo().window(arg_title);
}
}
return dObj;
}
}
WebDriver dObj = new FirefoxDriver();
dObj.manage().window().maximize();
dObj.get("https://www.naukri.com");
dObj.manage().timeouts().implicitlyWait(60,TimeUnit.SECONDS);
dObj=fn_SetFocus_According_Title(dObj,"Jobs - Recruitment - Job Search - Employment - Job Vacancies - Naukri.com");
dObj.findElement(By.xpath("//a[#id='login_Layer']")).click();
Make the following changes in your code:
Change to:
dObj.switchTo().window(windowhanldval);
Reduce the actual string of "arg_title" as you would be looking for this entire string within the String obtained by getTitle()
When you are already on that page trying to match the page title which means the focus is already on the actual page where we need to locate the Login button element. So remove the second switch () line entirely. Rather use "break" to come out if loop.
Let me know if these steps works for you.
The function below works for me.
public static void switchToWindow(String windowTitle)
{
for (String window : driver.getWindowHandles())
{
driver.switchTo().window(window);
if (driver.getTitle().equals(windowTitle))
{
return;
}
}
throw new InvalidParameterException("The window titled <" + windowTitle + "> does not exist.");
}
One issue you may run into is that when a new tab/window is created, you may need to wait for it to appear. To do that, you can use something like
int count = driver.getWindowHandles().size() + 1; // add 1 to the current window count
// do something that spawns a new window
new WebDriverWait(driver, 3).until(ExpectedConditions.numberOfWindowsToBe(count));
You don't need to return the WebDriver instance. It's the same driver instance you are already using. If the expected window title is not found, the function will throw an exception.
Hope this will work for you.
public void Parenthandle(WebDriver wb){
try {
String ParentPageHandle = wb.getWindowHandle();
for (String newPage : wb.getWindowHandles()) {
if (!ParentPageHandle.equals(newPage)) {
wb.switchTo().window(newPage);
}
}
} catch (Exception e) {
System.err.println(e.getMessage());
}
I have four windows forms namely, form1.vb, form2.vb, form3.vb, form4.vb.
And also i have one master page namely form5.vb. So i have rotate one by one above four windows forms in form5.vb with every 20 secs . how to do it ?
On a 20 second timer you can call 'BringToFront' on each form.
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.bringtofront.aspx
Basically, you create a timer and call the function BringToFront on each form.
In C#:
static int counter = 1;
static void StartRotating()
{
System.Windows.Forms.Timer myTimer = new System.Windows.Forms.Timer();
myTimer.Interval = 20000; // 20 seconds
myTimer.Tick += new EventHandler(TimerEventProcessor);
myTimer.Start();
}
private static void TimerEventProcessor(Object myObject,
EventArgs myEventArgs) {
// you could use a switch statement also
if(counter==1) form1.BringToFront();
if(counter==2) form2.BringToFront();
if(counter==3) form3.BringToFront();
if(counter==4) {
form4.BringToFront();
counter=0; //reset counter
}
counter++;
}
You need to keep an index to know which form is currently displayed and then in the timer elapsed event you can do this
formtoshow.TopMost = true;
formtoshow.BringToFront();
I'm developing windows phone 8 application.
I have two pages ; one of the page is the start up one ; Once the user open the application this page will appear and automatically after a specific time ; it will redirect the user to the main menu of the application .
How I can make an automatic redirection after specific time in WP8 ?
May be these lines of code helps you:
public partial class MainPage : PhoneApplicationPage
{
private DispatcherTimer timer;
// Constructor
public MainPage()
{
InitializeComponent();
timer = new DispatcherTimer();
//Set your specific time here using TimeSpan instance
timer.Interval = TimeSpan.FromSeconds(2);
timer.Tick += (s, e) => {
var frame = App.Current.RootVisual as PhoneApplicationFrame;
frame.Navigate(new Uri("/Page1.xaml", UriKind.Relative));
};
timer.Start();
}
}
Hope it helps.
Look at this answer for the a timer implementation. Only thing left to do is, when the timer finished, call a navigation method to navigate to the main menu.
I am using following code to open an IE browser from toolstipmenu_click() but getting this message as:
Error :No application is associated with the specified file for this operation
My code:
private void TutorialsToolStripMenuItem_Click(object sender, EventArgs e)
{
//Process.Start("http://www.microsoft.com/communities/newsgroups/en-us/default.aspx?dg=microsoft.public.speech.desktop&lang=en&cr=US");
Webbrowser();
}
private void Webbrowser()
{
System.Threading.Thread web = new System.Threading.Thread(new
System.Threading.ThreadStart(launchbrowser));
web.Start();
}
private void launchbrowser()
{
System.Diagnostics.Process.Start("http://www.microsoft.com/communities/newsgroups/en-us/default.aspx?dg=microsoft.public.speech.desktop&lang=en&cr=US");
}
Please assist me asap.
That technique has some known drawbacks as mentioned in this KB Article.
It could also be a problem with the querystring attached to the URL. Try launching it without the querystring and if that works you can proceed from there.
I would suggest you check the comment by Eric Law (of Microsoft) on the bottom of this answer to a very similar question.
Alternatively, there are a bunch of slightly different answers in that thread that will all do the job for you.
This question already has answers here:
Selenium IDE click() timeout
(2 answers)
Closed 3 years ago.
I have a GridView control with sorting enabled inside an UpdatePanel. I used Selenium IDE to record a test that clicks on the sort link of the table, but when I try to execute the test it get's stuck on the click command. Looking at the log I see:
[info] Executing: |click | link=Name | |
[error] Timed out after 30000ms
I haven't tried it with Selenium-RC yet, I don't know if it will be any different. I don't want Selenium to wait for anything. Any ideas of how to work around it?
Thanks!
when using selenium + Ajax (or the page just get refresh under certain conditions).
I usually use:
selenium.WaitForCondition
or I created the following code recently (the page uses frames).
public bool AccessElementsOnDynamicPage(string frame, Predicate<SeleniumWrapper> condition)
{
DateTime currentTime = DateTime.Now;
DateTime timeOutTime = currentTime.AddMinutes(6);
while (currentTime < timeOutTime)
{
try
{
SelectSubFrame(frame);
if (condition(this))
return true;
}
catch (SeleniumException)
{
//TODO: log exception
}
finally
{
currentTime = DateTime.Now;
}
}
return false;
}
public bool WaitUntilIsElementPresent(string frame, string locator)
{
return AccessElementsOnDynamicPage(frame, delegate(SeleniumWrapper w)
{
return w.IsElementPresent(locator);
});
}
public bool WaitUntilIsTextPresent(string frame, string pattern)
{
return AccessElementsOnDynamicPage(frame, delegate(SeleniumWrapper w)
{
return w.IsTextPresent(pattern);
});
}
Soon you will get to the point you will need selenium RC integrated on your development environment, for this I recommend you to read:
How can I make my Selenium tests less brittle?
It is around waiting but for specific elements that should be (or appear) on the page.