Protractor中browser.get()方法无法获取正确URL的问题求助
Hey Diego, let's work through that browser.get() URL issue you're hitting while testing your Angular 4.4.6 template-driven form with Protractor. I’ve dealt with similar headaches before, so here are targeted checks to get you back on track:
baseUrl First, head to your protractor.conf.js file and confirm the baseUrl matches your Angular app’s running URL. If this is misconfigured, relative paths in browser.get() will point to the wrong place. Here’s what a correct setup might look like:
exports.config = { baseUrl: 'http://localhost:4200/', // Ensure this matches where ng serve hosts your app specs: ['./e2e/**/*.e2e-spec.ts'], // ... other configuration options };
If you’re unsure, try using an absolute path directly in browser.get() to rule out baseUrl issues:
browser.get('http://localhost:4200/your-form-page');
Protractor can’t reach a URL that doesn’t exist—so confirm you’ve started your app first with ng serve (or npm start if that’s your setup script) before running your Protractor tests. I’ve definitely forgotten this step more times than I’d admit!
If your app has route guards, default redirects, or typos in routes, browser.get() might be sending you to an unexpected page. Add a quick debug log to see your actual URL after navigation:
browser.get('/your-form-path').then(() => { browser.getCurrentUrl().then(currentUrl => { console.log('Protractor landed on:', currentUrl); }); });
This will tell you if you’re being redirected elsewhere, which points to a routing issue in your Angular app rather than a Protractor problem.
Angular 4.4.6 works best with Protractor versions in the 5.x to 6.x range. Check your package.json for your Protractor version—if it’s too new or old, update it with:
npm install protractor@5.4.4 --save-dev
Also, make sure your webdriver is up to date by running:
webdriver-manager update
Mismatched versions between Protractor, Selenium Webdriver, and Angular can cause weird navigation bugs.
Template-driven forms depend on Angular’s change detection, so Protractor might try to navigate before the app is fully stabilized. Even though Protractor waits for Angular by default, you can explicitly wait for your form to load to be safe:
const EC = protractor.ExpectedConditions; const form = element(by.css('form')); browser.get('/your-form-page'); browser.wait(EC.presenceOf(form), 5000, 'Form took too long to load');
This ensures you’re only interacting with the page once your form is actually present.
It sounds simple, but a tiny typo in your URL path (like /contactform instead of /contact-form) will break navigation. Cross-reference the path in browser.get() with the route defined in your Angular routing module to make sure they match exactly.
内容的提问来源于stack exchange,提问作者Diego

