/* * YahooLogin.cpp * TSMFCAPI v1.3.0 * 15 March 2005 * © Quality Forge, 2004-2005 */ /***************************************************************************** YahooLogin.cpp Sample MFC/C++ test-script demonstrating web page login. This script will login the user 'smith030615' to My Yahoo!, validate some text on the login page, attempt to validate some nonexistent text to show an error message example, logout the user and validate text on the logout page. ---------------------------------------------------------------------------- Description of the System on which the script was recorded: Windows 2000, Screen Size: 1024 x 768, Color: High Color (16 bit) *****************************************************************************/ #include //for AfxWinInit & AfxMessageBox #include "YahooLogin.h" //YahooLogin class definition /** * $param data: no data required */ int YahooLogin::Run(CString data) { //initialize try { //initialize test //note: the \Reports directory must already exist in the // same directory where this java class file exists int initialized = InitSession("reports\\mfcYahooLogin"); //to run without generating any TestSmith reports use: //int initialized = initSession(); if (initialized == FALSE) { //handle error... //use getLastTSError() for errors generated by TSAPI //use getLastCError() for errors generated by CSmith return -1; } } catch (InSessionException e) { //handle error, e.g. printf("[%s]\n", e.reason); return -1; } try { //set environment //... //Launch an IE Browser //IEXPLORE.EXE is assumed to reside in "C:\Program Files\Internet Explorer" //Change the path as appropriate for your own system. int envSet = SetEnv("LAUNCH='C:/Program Files/Internet Explorer/IEXPLORE.EXE'"); if (envSet == FALSE) { //handle error... //use getLastTSError() for errors generated by TSAPI //use getLastCError() for errors generated by CSmith printf((LPTSTR)"[%s]\n", GetLastTSError()); return -1; } //run test int lasterr = 0; //... //write a remark to the report Noop("remark='MFC/C++ Yahoo Login Test'"); //identify the IE browser window //note approximate match on the title lasterr = WndInit("id=1 wtitle~='Microsoft Internet Explorer' wclass=IEFrame wrect=MAXIMIZED"); if (lasterr == NO_ERR) { //id and wnum are the same for all calls, so make a constant //note: update the wnum param if the playback issues multiple warnings. const CString window_id = "id=1 wnum={wnum} "; //range and sync params are the same for most calls, so make a constant const CString common_params = " hrange=100 zsync=s10 zinterval=s2"; //variable for other params CString params; //Navigate to my.yahoo.com params = " url=http://my.yahoo.com/ "; lasterr = HtmlNavigate(window_id + params); //Click sign-in link params = " htag=A hidx=33 hhref~='login.yahoo.com/config/login','src=my' hvistext='Sign In' "; lasterr = HtmlLink(window_id + params + common_params); //Enter name params = " htag=INPUT hidx=84 htype=text hname=login hvalue=smith030615 "; lasterr = HtmlText(window_id + params + common_params); //Enter (obfuscated) password params = " htag=INPUT hidx=89 htype=password hvalue=[!OBF!]/!*%2B&!#.%2B!# "; lasterr = HtmlText(window_id + params + common_params); //click Submit button params = " htag=INPUT hidx=93 htype=submit hvalue='Sign in' "; lasterr = HtmlInput(window_id + params + common_params); //Validate the greeting/user name. params = " htag=P hidx=30 hoffset=0 remark='Validate the greeting [Welcome, smith030615].' hvistext='Welcome%cm smith030615' "; lasterr = ValHtmlText(window_id + params + common_params); //This validation is intended to fail CString non_existent = "[non-existent text !@#$%^&*]"; params = " htag=B hidx=33 hoffset=0" " remark='validation for " + non_existent + " is intended to fail.'" " remark2='validation for " + non_existent + " will have failed.'" " hvistext='" + non_existent + "' zsync=s5 "; lasterr = ValHtmlText(window_id + params); //check for error (this can be done after any - or every - command) if (lasterr > NO_ERR) { //handle error } //Click the Sign Out link params = " htag=A hidx=33 hhref~='http://login.yahoo.com','logout=1' hvistext='Sign Out' "; lasterr = HtmlLink(window_id + params + common_params); //Validate that sign out was successful. (1 of 2 possible strings) params = " htag=B hidx=49 hoffset=0 hvistext='You have signed out of the Yahoo! network' "; lasterr = ValHtmlText(window_id + params + common_params); //Close the IE Browser lasterr = WndClose("id=1"); } //end test int exit_ok = ExitSession(); if (exit_ok == 0) { //handle error... //use getLastCError() for errors generated by TSAPI //use getLastJError() for errors generated by JSmith } //full test ran without exceptions, so return GetExitCode(); } catch (UninitializedException e) { //handle error, e.g. printf((LPTSTR)"[%s]\n", e.reason); return -1; } } //============================================================================= //TEST int _tmain(int argc, TCHAR* argv[], TCHAR* envp[]) { //initialize MFC (and print error on failure) if (!AfxWinInit(::GetModuleHandle(0), 0, ::GetCommandLine(), 0)) { printf("Fatal Error: MFC initialization failed"); return -1; } YahooLogin yahooLogin; int exitCode = yahooLogin.Run(); CString result; result.Format("YahooLogin exit code = %d", exitCode); printf("\n%s\n", result); //print to stdout AfxMessageBox(result, MB_OK|MB_ICONINFORMATION|MB_SYSTEMMODAL); //show in dialog box MFCSmith::DisplayLastReport(); return exitCode; } //=============================================================================