As someone who worked at a dealership a while ago, I have to second that you absolutely do not want to set foot in the dealership until you have a deal you like with the exact options/color/trim/etc in writing and preferably with a small (insist on 100% refundable) deposit. The dealership I worked for was shady and wouldn't hesitate to tell potential customers that they had a particular vehicle in stock even if they didn't. I would get people calling in from 100 miles away asking if we had a car, check with the sales manager to see if it was in stock, he'd say it isn't but ask if they had a deposit, and if not he'd straight up tell me to lie to them. If there's no deposit, than they can say "Oh we had one, but we sold it/let someone test drive it while you were on the way here." and try to flip you onto another car. Their level of closing rate is much higher when you are actually in the dealership, because they have other cars in stock for you to look at, and if you take a test drive you have an additional emotional investment in the car you're buying. MOST dealerships probably aren't this shady, but there is little way to tell. This level of dishonesty is why I immediately left the car selling business. It was a nightmare.
Like others are saying, get several dealers to quote against each other, when you find the deal you like and are serious about, put a small 100% refundable deposit (again insist on this) on it with the exact specs/options in writing over the phone/via fax. I second the notion of not giving them your email address/actual phone number if you can avoid it because it immediately goes into their lead system to spam you even years later. Ideally you want to be a cash buyer or at least have your own financing before you haggle so they can't play games with numbers on the backend. If at any time you suspect something fishy is going on, walk out of the dealership and use another one.
If you do go in person, which again, I don't recommend, I second the poster that said always leave after the first negotiation. They will be more inclined to give you a better deal to get you back into the dealership (doesn't always work this way, but if they are trying to hit quotas, and they often are, it will work). I got $4K off invoice on my WRX this way many years ago.
Also, never go in for the extras they try to add during closing. You want gap insurance, and that is all. No paint protectant bullshit, pinstripes or any of that. The only exception I might consider for this is the pre-paid maintenance plans some of the luxury manufacturers like BMW and Lexus are doing now, as in some cases those are apparently worth it.